Exemplo n.º 1
0
 public ConfigToken(ConfigTokenType type, CharPos fromPos, CharPos toPos, string text)
 {
     Type    = type;
     FromPos = fromPos;
     ToPos   = toPos;
     Text    = text;
 }
Exemplo n.º 2
0
        ConfigToken NewFixedToken(ConfigTokenType type, int startPosition, int length, string text)
        {
            var fromPos = NewCharPos(startPosition);
            var toPos   = length > 1 ? NewCharPos(startPosition + length - 1) : fromPos;

            return(new ConfigToken(type, fromPos, toPos, text));
        }
Exemplo n.º 3
0
        State Parse(IEnumerable <Type_Terminal> tokens, int initialState)
        {
            List <State> stack = new List <State>();

            {
                State init = new State();
                init.state = initialState;
                stack.Add(init);
            }

            int state = initialState;

            using (IEnumerator <Type_Terminal> enumerator = tokens.GetEnumerator())
            {
                bool haveToken = enumerator.MoveNext();

                while (haveToken)
                {
                    Type_Terminal   token = enumerator.Current;
                    ConfigTokenType type  = GetTokenType(token);

                    if (type == ConfigTokenType.Error)
                    {
                        state = ReduceError(stack, enumerator);
                    }
                    else
                    {
                        int offset = _transitionTable[state];
                        int action = offset <= 33 ? offset : _transitionTable[offset + (int)(type & ColumnMask)];

                        if (action > 33)
                        {
                            State newState = new State();
                            newState.state          = state = action - 33;
                            newState.value_Terminal = token;

                            stack.Add(newState);
                            haveToken = enumerator.MoveNext();
                        }
                        else if (action > 1)
                        {
                            int reductionId = action - 2;
                            state = Reduce(reductionId, stack);
                        }
                        else if (action == 0 || type != ConfigTokenType.EOF)
                        {
                            state = ReduceError(stack, enumerator);
                        }
                        else
                        {
                            return(stack[1]);
                        }
                    }
                }
            }

            throw new InvalidOperationException("ran out of tokens, somehow");
        }
Exemplo n.º 4
0
        bool CanBeFollowedBy(List <State> stack, int tosIndex, int startState, ConfigTokenType type)
        {
            List <int> overStack = new List <int>();

            overStack.Add(startState);

            int state = startState;

            while (true)
            {
                int offset = _transitionTable[state];
                int action = offset <= 33 ? offset : _transitionTable[offset + (int)(type & ColumnMask)];

                if (action == 0)
                {
                    return(false);
                }
                if (action > 33 || action == 1)
                {
                    return(true);
                }

                int count;
                int gotoIndex;

                switch (action - 2)
                {
                case 5:                         // <Config> -> <Config> <Production>
                    count     = 2;
                    gotoIndex = 0;
                    break;

                case 4:                         // <Config> -> <Config> Error
                    count     = 2;
                    gotoIndex = 0;
                    break;

                case 1:                         // <Config> -> <ConfigSettings>
                    count     = 1;
                    gotoIndex = 0;
                    break;

                case 8:                         // <ConfigSettings> -> <ConfigSettings> <Setting>
                    count     = 2;
                    gotoIndex = 1;
                    break;

                case 9:                         // <ConfigSettings> -> <ConfigSettings> <Using>
                    count     = 2;
                    gotoIndex = 1;
                    break;

                case 7:                         // <ConfigSettings> -> <ConfigSettings> <EntryPoint>
                    count     = 2;
                    gotoIndex = 1;
                    break;

                case 6:                         // <ConfigSettings> -> <ConfigSettings> Error
                    count     = 2;
                    gotoIndex = 1;
                    break;

                case 2:                         // <ConfigSettings> -> Error
                    count     = 1;
                    gotoIndex = 1;
                    break;

                case 0:                         // <ConfigSettings> ->
                    count     = 0;
                    gotoIndex = 1;
                    break;

                case 20:                         // <Setting> -> Label Assign <SettingValue> Semicolon
                    count     = 4;
                    gotoIndex = 1;
                    break;

                case 14:                         // <Setting> -> Label Error Semicolon
                    count     = 3;
                    gotoIndex = 1;
                    break;

                case 25:                         // <Using> -> Using Label Assign String Semicolon
                    count     = 5;
                    gotoIndex = 2;
                    break;

                case 15:                         // <Using> -> Using Error Semicolon
                    count     = 3;
                    gotoIndex = 2;
                    break;

                case 13:                         // <EntryPoint> -> Entry NonTerminal Semicolon
                    count     = 3;
                    gotoIndex = 0;
                    break;

                case 12:                         // <EntryPoint> -> Entry Error Semicolon
                    count     = 3;
                    gotoIndex = 0;
                    break;

                case 24:                         // <Production> -> NonTerminal <ProductionTypeDef> Becomes <RuleList> Semicolon
                    count     = 5;
                    gotoIndex = 0;
                    break;

                case 10:                         // <Production> -> NonTerminal Error Semicolon
                    count     = 3;
                    gotoIndex = 0;
                    break;

                case 16:                         // <ProductionTypeDef> -> OpenParen Label CloseParen
                    count     = 3;
                    gotoIndex = 0;
                    break;

                case 3:                         // <ProductionTypeDef> ->
                    count     = 0;
                    gotoIndex = 0;
                    break;

                case 27:                         // <RuleList> -> <RuleList> Pipe <Rule>
                    count     = 3;
                    gotoIndex = 2;
                    break;

                case 18:                         // <RuleList> -> <Rule>
                    count     = 1;
                    gotoIndex = 2;
                    break;

                case 17:                         // <Rule> -> <SegmentList>
                    count     = 1;
                    gotoIndex = 1;
                    break;

                case 22:                         // <Rule> -> <SegmentList> <Command>
                    count     = 2;
                    gotoIndex = 1;
                    break;

                case 23:                         // <SegmentList> -> <SegmentList> <Segment>
                    count     = 2;
                    gotoIndex = 0;
                    break;

                case 19:                         // <SegmentList> -> Error
                    count     = 1;
                    gotoIndex = 0;
                    break;

                case 11:                         // <SegmentList> ->
                    count     = 0;
                    gotoIndex = 0;
                    break;

                case 21:                         // <Segment> -> <RawSegment>
                    count     = 1;
                    gotoIndex = 1;
                    break;

                case 26:                         // <Segment> -> <RawSegment> <SegmentModifier>
                    count     = 2;
                    gotoIndex = 1;
                    break;

                case 31:                         // <Command> -> OpenBrace TargetValue Assign <CommandExpression> Semicolon CloseBrace
                    count     = 6;
                    gotoIndex = 0;
                    break;

                case 28:                         // <Command> -> OpenBrace Error CloseBrace
                    count     = 3;
                    gotoIndex = 0;
                    break;

                case 29:                         // <CommandExpression> -> ArgumentValue
                    count     = 1;
                    gotoIndex = 0;
                    break;

                case 30:                         // <CommandExpression> -> Null
                    count     = 1;
                    gotoIndex = 0;
                    break;

                default: throw new InvalidOperationException("unknown reduction");
                }

                if (count >= overStack.Count)
                {
                    tosIndex = tosIndex - count + overStack.Count;
                    overStack.Clear();

                    state = stack[tosIndex].state;
                }
                else if (count > 0)
                {
                    overStack.RemoveRange(overStack.Count - count, count);

                    state = overStack[overStack.Count - 1];
                }

                overStack.Add(state = _transitionTable[_transitionTable[state + 57] + gotoIndex]);
            }
        }
Exemplo n.º 5
0
        int ReduceError(List <State> stack, IEnumerator <Type_Terminal> enumerator)
        {
            Type_Terminal errorToken = enumerator.Current;

            if (GetTokenType(enumerator.Current) != ConfigTokenType.EOF)
            {
                if (!enumerator.MoveNext())
                {
                    throw new InvalidOperationException("ran out of tokens while attempting to recover from a parse error.");
                }
            }

            bool[] failed = new bool[19];

            do
            {
                int state  = stack[stack.Count - 1].state;
                int offset = _transitionTable[state];
                int action = offset <= 33 ? offset : _transitionTable[offset + (int)(ConfigTokenType.Error & ColumnMask)];

                if (action == 0 || action > 33)
                {
                    break;
                }
                else
                {
                    int reductionId = action - 2;
                    state = Reduce(reductionId, stack);
                }
            }while (true);

            do
            {
                ConfigTokenType nextType = GetTokenType(enumerator.Current);

                if (!failed[(int)(nextType & ColumnMask)])
                {
                    for (int i = stack.Count - 1; i >= 0; i--)
                    {
                        int state  = stack[i].state;
                        int offset = _transitionTable[state];
                        if (offset <= 33)
                        {
                            continue;
                        }

                        int action = _transitionTable[offset + (int)(ConfigTokenType.Error & ColumnMask)] - 33;

                        if (action <= 0)
                        {
                            continue;
                        }
                        if (!CanBeFollowedBy(stack, i, action, nextType))
                        {
                            continue;
                        }

                        State newState = new State();
                        newState.state          = action;
                        newState.value_Terminal = errorToken;

                        stack.RemoveRange(i + 1, stack.Count - i - 1);
                        stack.Add(newState);
                        return(action);
                    }

                    failed[(int)(nextType & ColumnMask)] = true;
                }

                if (nextType == ConfigTokenType.EOF)
                {
                    throw new InvalidOperationException("unexpected token: " + GetTokenType(errorToken));
                }
            }while (enumerator.MoveNext());

            throw new InvalidOperationException("ran out of tokens while attempting to recover from a parse error.");
        }
Exemplo n.º 6
0
        bool CanBeFollowedBy(List <State> stack, int tosIndex, int startState, ConfigTokenType type)
        {
            List <int> overStack = new List <int>();

            overStack.Add(startState);

            int state = startState;

            while (true)
            {
                int offset = _transitionTable[state];
                int action = offset <= 16 ? offset : _transitionTable[offset + (int)type];

                if (action == 0)
                {
                    return(false);
                }
                if (action > 16 || action == 1)
                {
                    return(true);
                }

                int count;
                int gotoIndex;

                switch (action - 2)
                {
                case 4:                         // <Config> -> <Config> <State>
                    count     = 2;
                    gotoIndex = 0;
                    break;

                case 3:                         // <Config> -> <Config> Error
                    count     = 2;
                    gotoIndex = 0;
                    break;

                case 1:                         // <Config> -> <ConfigSettings>
                    count     = 1;
                    gotoIndex = 0;
                    break;

                case 6:                         // <ConfigSettings> -> <ConfigSettings> <Setting>
                    count     = 2;
                    gotoIndex = 1;
                    break;

                case 5:                         // <ConfigSettings> -> <ConfigSettings> Error
                    count     = 2;
                    gotoIndex = 1;
                    break;

                case 2:                         // <ConfigSettings> -> Error
                    count     = 1;
                    gotoIndex = 1;
                    break;

                case 0:                         // <ConfigSettings> ->
                    count     = 0;
                    gotoIndex = 1;
                    break;

                case 10:                         // <Setting> -> Label Assign <SettingValue> Semicolon
                    count     = 4;
                    gotoIndex = 0;
                    break;

                case 8:                         // <Setting> -> Label Error Semicolon
                    count     = 3;
                    gotoIndex = 0;
                    break;

                case 12:                         // <State> -> StateKeyword Label OpenBrace <RuleList> CloseBrace
                    count     = 5;
                    gotoIndex = 0;
                    break;

                case 11:                         // <RuleList> -> <RuleList> <Rule>
                    count     = 2;
                    gotoIndex = 0;
                    break;

                case 9:                         // <RuleList> -> Error
                    count     = 1;
                    gotoIndex = 0;
                    break;

                case 7:                         // <RuleList> ->
                    count     = 0;
                    gotoIndex = 0;
                    break;

                case 14:                         // <Rule> -> Regex OpenBrace TokenKeyword Label Semicolon CloseBrace
                    count     = 6;
                    gotoIndex = 0;
                    break;

                case 13:                         // <Rule> -> Regex OpenBrace Error CloseBrace
                    count     = 4;
                    gotoIndex = 0;
                    break;

                default: throw new InvalidOperationException("unknown reduction");
                }

                if (count >= overStack.Count)
                {
                    tosIndex = tosIndex - count + overStack.Count;
                    overStack.Clear();

                    state = stack[tosIndex].state;
                }
                else if (count > 0)
                {
                    overStack.RemoveRange(overStack.Count - count, count);

                    state = overStack[overStack.Count - 1];
                }

                overStack.Add(state = _transitionTable[_transitionTable[state + 29] + gotoIndex]);
            }
        }