Exemplo n.º 1
0
        /*
         *      private bool SkipToEndOfLine()
         *      {
         *              bool result = tokenizer.SkipToChar('\n');
         *              if (! result)
         *              {
         *                      FireEOFError();
         *              }
         *              return result;
         *      }
         */

        private TerminalToken SkipAfterCommentEnd()
        {
            int           commentDepth = 1;
            TerminalToken token        = null;

            while (commentDepth > 0)
            {
                token = tokenizer.RetrieveToken();
                if (token.Symbol is SymbolCommentEnd)
                {
                    commentDepth--;
                }
                else if (token.Symbol is SymbolCommentStart)
                {
                    commentDepth++;
                }
                else if (token.Symbol is SymbolEnd)
                {
                    FireEOFError();
                    break;
                }
            }
            if (commentDepth == 0)
            {
                return(token);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
 private bool ProcessCommentStart(TerminalToken token)
 {
     if (OnCommentRead == null)
     {
         return(SkipAfterCommentEnd() != null);
     }
     else
     {
         Location      start      = this.tokenizer.GetCurrentLocation();
         TerminalToken commentEnd = SkipAfterCommentEnd();
         bool          result     = commentEnd != null;
         if (result)
         {
             Location             end     = this.tokenizer.GetCurrentLocation();
             string               str     = this.tokenizer.GetInput();
             int                  len     = end.Position - start.Position;
             string               comment = str.Substring(start.Position, len - commentEnd.Text.Length);
             CommentReadEventArgs args    = new CommentReadEventArgs(token.Text + comment,
                                                                     comment,
                                                                     false);
             OnCommentRead(this, args);
         }
         return(result);
     }
 }
Exemplo n.º 3
0
        private TerminalToken GetLookahead()
        {
            if (lookahead != null)
            {
                return(lookahead);
            }
            do
            {
                TerminalToken token = tokenizer.RetrieveToken();
                if (token.Symbol is SymbolCommentLine)
                {
                    if (!ProcessCommentLine(token))
                    {
                        continueParsing = false;
                    }
                }
                else if (token.Symbol is SymbolCommentStart)
                {
                    if (!ProcessCommentStart(token))
                    {
                        continueParsing = false;
                    }
                }
                else if (token.Symbol is SymbolWhiteSpace)
                {
                    if (!ProcessWhiteSpace(token))
                    {
                        continueParsing = false;
                    }
                }
                else if (token.Symbol is SymbolError)
                {
                    if (!ProcessError(token))
                    {
                        continueParsing = false;
                    }
                }
                else
                {
                    lookahead = token;
                }
                if (!continueParsing)
                {
                    break;
                }
            } while (lookahead == null);

            if ((lookahead != null) && (OnTokenRead != null))
            {
                TokenReadEventArgs args = new TokenReadEventArgs(lookahead);
                OnTokenRead(this, args);
                if (args.Continue == false)
                {
                    continueParsing = false;
                    lookahead       = null;
                }
            }
            return(lookahead);
        }
Exemplo n.º 4
0
 public ParseErrorEventArgs(TerminalToken unexpectedToken,
                            SymbolCollection expectedTokens)
 {
     this.unexpectedToken = unexpectedToken;
     this.expectedTokens  = expectedTokens;
     this.contin          = ContinueMode.Stop;
     this.nextToken       = null;
 }
Exemplo n.º 5
0
        private void FireEOFError()
        {
            TerminalToken eofToken = new TerminalToken(SymbolCollection.EOF,
                                                       SymbolCollection.EOF.Name,
                                                       tokenizer.GetCurrentLocation());

            FireParseError(eofToken);
        }
Exemplo n.º 6
0
 private void Reset()
 {
     stateStack = new StateStack();
     stateStack.Push(startState);
     tokenStack      = new TokenStack();
     lookahead       = null;
     continueParsing = true;
     accepted        = false;
 }
Exemplo n.º 7
0
 private void DoShift(TerminalToken token, ShiftAction action)
 {
     stateStack.Push(action.State);
     tokenStack.Push(token);
     lookahead = null;
     if (OnShift != null)
     {
         OnShift(this, new ShiftEventArgs(token, action.State));
     }
 }
Exemplo n.º 8
0
 private bool ProcessError(TerminalToken token)
 {
     if (OnTokenError != null)
     {
         TokenErrorEventArgs e = new TokenErrorEventArgs(token);
         OnTokenError(this, e);
         return(e.Continue);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 9
0
 private void FireParseError(TerminalToken token)
 {
     if (OnParseError != null)
     {
         ParseErrorEventArgs e =
             new ParseErrorEventArgs(token, FindExpectedTokens());
         OnParseError(this, e);
         continueParsing = e.Continue != ContinueMode.Stop;
         lookahead       = e.NextToken;
         if ((e.NextToken != null) && (e.Continue == ContinueMode.Insert))
         {
             tokenizer.SetCurrentLocation(token.Location);
         }
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Parse the input with tokens and rules.
        /// </summary>
        /// <param name="input">The source input</param>
        /// <returns>The nonterminal token that the input has been reduced to.
        /// Null if the parse has failed.</returns>
        public NonterminalToken Parse(System.String input)
        {
            Reset();
            tokenizer.SetInput(input);

            while (continueParsing)
            {
                TerminalToken token = GetLookahead();
                if (token != null)
                {
                    ParseTerminal(token);
                }
            }
            if (accepted)
            {
                return((NonterminalToken)tokenStack.Pop());
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 11
0
 private bool ProcessCommentLine(TerminalToken token)
 {
     if (OnCommentRead == null)
     {
         tokenizer.SkipToChar('\n');
         //SkipToEndOfLine();
         return(true);
     }
     else
     {
         Location start = this.tokenizer.GetCurrentLocation();
         tokenizer.SkipToChar('\n');
         //SkipToEndOfLine();
         Location             end     = this.tokenizer.GetCurrentLocation();
         string               str     = this.tokenizer.GetInput();
         int                  len     = end.Position - start.Position;
         string               comment = str.Substring(start.Position, len);
         CommentReadEventArgs args    = new CommentReadEventArgs(token.Text + comment,
                                                                 comment,
                                                                 true);
         OnCommentRead(this, args);
         return(true);
     }
 }
Exemplo n.º 12
0
        private void ParseTerminal(TerminalToken token)
        {
            State currentState = stateStack.Peek();

            Action action = currentState.Actions.Get(token.Symbol);

            if (action is ShiftAction)
            {
                DoShift(token, (ShiftAction)action);
            }
            else if (action is ReduceAction)
            {
                DoReduce(token, (ReduceAction)action);
            }
            else if (action is AcceptAction)
            {
                DoAccept(token, (AcceptAction)action);
            }
            else
            {
                continueParsing = false;
                FireParseError(token);
            }
        }
Exemplo n.º 13
0
 public ShiftEventArgs(TerminalToken token, State newState)
 {
     this.token    = token;
     this.newState = newState;
 }
Exemplo n.º 14
0
 public TokenErrorEventArgs(TerminalToken token)
 {
     this.token  = token;
     this.contin = false;
 }
Exemplo n.º 15
0
 public TokenReadEventArgs(TerminalToken token)
 {
     this.token = token;
     contin     = true;
 }