コード例 #1
0
ファイル: ParserInterpreter.cs プロジェクト: tnsr1/antlr4cs
        public ParserInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, ATN atn, ITokenStream input)
            : base(input)
        {
            this.grammarFileName = grammarFileName;
            this.atn             = atn;
#pragma warning disable 612 // Type or member is obsolete
            this.tokenNames = new string[atn.maxTokenType];
            for (int i = 0; i < tokenNames.Length; i++)
            {
                tokenNames[i] = vocabulary.GetDisplayName(i);
            }
#pragma warning restore 612 // Type or member is obsolete
            this.ruleNames  = ruleNames.ToArray();
            this.vocabulary = vocabulary;
            // identify the ATN states where pushNewRecursionContext() must be called
            this.pushRecursionContextStates = new BitSet(atn.states.Count);
            foreach (ATNState state in atn.states)
            {
                if (!(state is StarLoopEntryState))
                {
                    continue;
                }
                if (((StarLoopEntryState)state).precedenceRuleDecision)
                {
                    this.pushRecursionContextStates.Set(state.stateNumber);
                }
            }
            // get atn simulator that knows how to do predictions
            Interpreter = new ParserATNSimulator(this, atn);
        }
コード例 #2
0
 private string ToText(IParseTree tree, IVocabulary names)
 {
     if (tree is TerminalNodeImpl token)
     {
         return(String.Concat("\"", token.GetText(), "\" - ", names.GetDisplayName(token.Symbol.Type)));
     }
     return(tree.GetType().Name.ToString());
 }
コード例 #3
0
ファイル: LanguageRepl.cs プロジェクト: k2green/Interpreter
        private void RenderToken(IToken token, IVocabulary vocab)
        {
            switch (vocab.GetDisplayName(token.Type))
            {
            case "INTEGER":
            case "DOUBLE":
            case "BOOLEAN":
                Console.ForegroundColor = Color.Aquamarine;
                break;

            case "STRING":
                Console.ForegroundColor = Color.FromArgb(214, 157, 133);
                break;

            case "FUNCTION":
            case "DECL_VARIABLE":
                Console.ForegroundColor = Color.Violet;
                break;

            case "IF":
            case "ELSE":
            case "WHILE":
            case "FOR":
            case "BREAK":
            case "CONTINUE":
                Console.ForegroundColor = Color.MediumVioletRed;
                break;

            case "TYPE_NAME":
                Console.ForegroundColor = Color.Orange;
                break;

            case "IDENTIFIER":
                Console.ForegroundColor = Color.Yellow;
                break;

            case "UNKNOWN":
                Console.ForegroundColor = Color.Red;
                break;

            default:
                Console.ForegroundColor = DEFAULT_COLOR;
                break;
            }

            Console.Write(token.Text);
        }
コード例 #4
0
 protected internal virtual string ElementName(IVocabulary vocabulary, int a)
 {
     if (a == TokenConstants.Eof)
     {
         return("<EOF>");
     }
     else
     {
         if (a == TokenConstants.Epsilon)
         {
             return("<EPSILON>");
         }
         else
         {
             return(vocabulary.GetDisplayName(a));
         }
     }
 }
コード例 #5
0
ファイル: LexerInterpreter.cs プロジェクト: sharwell/antlr4cs
        public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable<string> ruleNames, IEnumerable<string> modeNames, ATN atn, ICharStream input)
            : base(input)
        {
            if (atn.grammarType != ATNType.Lexer)
            {
                throw new ArgumentException("The ATN must be a lexer ATN.");
            }
            this.grammarFileName = grammarFileName;
            this.atn = atn;
#pragma warning disable 612 // 'fieldName' is obsolete
            this.tokenNames = new string[atn.maxTokenType];
            for (int i = 0; i < tokenNames.Length; i++)
            {
                tokenNames[i] = vocabulary.GetDisplayName(i);
            }
#pragma warning restore 612
            this.ruleNames = ruleNames.ToArray();
            this.modeNames = modeNames.ToArray();
            this.vocabulary = vocabulary;
            this._interp = new LexerATNSimulator(this, atn);
        }
コード例 #6
0
        public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, IEnumerable <string> modeNames, ATN atn, ICharStream input)
            : base(input)
        {
            if (atn.grammarType != ATNType.Lexer)
            {
                throw new ArgumentException("The ATN must be a lexer ATN.");
            }
            this.grammarFileName = grammarFileName;
            this.atn             = atn;
#pragma warning disable 612 // 'fieldName' is obsolete
            this.tokenNames = new string[atn.maxTokenType];
            for (int i = 0; i < tokenNames.Length; i++)
            {
                tokenNames[i] = vocabulary.GetDisplayName(i);
            }
#pragma warning restore 612
            this.ruleNames  = ruleNames.ToArray();
            this.modeNames  = modeNames.ToArray();
            this.vocabulary = vocabulary;
            this._interp    = new LexerATNSimulator(this, atn);
        }
コード例 #7
0
 protected internal virtual string GetEdgeLabel(int i)
 {
     return(vocabulary.GetDisplayName(i));
 }
コード例 #8
0
        /// <summary>
        /// Notifies that there has been a syntax error.
        /// </summary>
        /// <param name="recognizer">The state of the parser when the syntax error was found.</param>
        /// <param name="offendingSymbol">The offending token in the input stream.</param>
        /// <param name="line">The line number of the syntax error.</param>
        /// <param name="charPositionInLine">The column number of the syntax error.</param>
        /// <param name="msg">The message to emit.</param>
        /// <param name="e">The base exception generated that lead to the reporting of an error.</param>
        public override void SyntaxError(IRecognizer recognizer, IToken offendingSymbol, int line,
                                         int charPositionInLine, string msg, RecognitionException e)
        {
            base.SyntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);

            // Create error params
            string    message   = msg;
            IToken    symbol    = offendingSymbol;
            ErrorType errorType = ErrorType.GenericParserError;

            // Get parser and dictionary
            ChoopParser parser = recognizer as ChoopParser;

            if (parser != null && parser.State > -1)
            {
                // Parser is a ChoopParser and an automatic parser error was thrown

                // Get lexer rules
                IVocabulary vocabulary = ChoopLexer.DefaultVocabulary;

                // Get expected tokens
                List <int> expectedTokenTypes = parser.GetExpectedTokensWithinCurrentRule().ToIntegerList();
                string[]   expectedTokens     = expectedTokenTypes.Select(t => vocabulary.GetDisplayName(t)).ToArray();

                if (expectedTokens.Length == 1)
                {
                    // Simple case - only 1 expected token
                    message   = expectedTokens[0] + " expected";
                    errorType = ErrorType.TokenMissing;
                }
                else
                {
                    // Multiple potential expected tokens
                    // Use analysis to work out best message

                    if (e == null)
                    {
                        // No exception - generic error

                        // Assume extraneous input
                        message = string.Concat("Expected {", string.Join(", ", expectedTokens), "} but found '",
                                                offendingSymbol.Text, "'");
                        errorType = ErrorType.ExtraneousToken;
                    }
                    else
                    {
                        // Exception occured

                        NoViableAltException exception = e as NoViableAltException;
                        if (exception != null)
                        {
                            // Could not match input to token
                            symbol  = exception.StartToken;
                            message = string.Concat("Expected {", string.Join(", ", expectedTokens), "} but found '",
                                                    symbol.Text, "'");
                            errorType = ErrorType.NoViableAlternative;
                        }
                    }
                }
            }

            // Add error to collection
            _errorCollection.Add(
                new CompilerError(
                    message,
                    errorType,
                    line,
                    charPositionInLine,
                    symbol.StartIndex,
                    symbol.StopIndex,
                    symbol.Text,
                    _fileName
                    )
                );
        }
コード例 #9
0
ファイル: IntervalSet.cs プロジェクト: antlr/antlr4
 protected internal virtual string ElementName(IVocabulary vocabulary, int a)
 {
     if (a == TokenConstants.EOF)
     {
         return "<EOF>";
     }
     else
     {
         if (a == TokenConstants.EPSILON)
         {
             return "<EPSILON>";
         }
         else
         {
             return vocabulary.GetDisplayName(a);
         }
     }
 }
コード例 #10
0
        public ParserInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable<string> ruleNames, ATN atn, ITokenStream input)
            : base(input)
        {
            this.grammarFileName = grammarFileName;
            this.atn = atn;
#pragma warning disable 612 // Type or member is obsolete
            this.tokenNames = new string[atn.maxTokenType];
            for (int i = 0; i < tokenNames.Length; i++)
            {
                tokenNames[i] = vocabulary.GetDisplayName(i);
            }
#pragma warning restore 612 // Type or member is obsolete
            this.ruleNames = ruleNames.ToArray();
            this.vocabulary = vocabulary;
            // identify the ATN states where pushNewRecursionContext() must be called
            this.pushRecursionContextStates = new BitSet(atn.states.Count);
            foreach (ATNState state in atn.states)
            {
                if (!(state is StarLoopEntryState))
                {
                    continue;
                }
                if (((StarLoopEntryState)state).precedenceRuleDecision)
                {
                    this.pushRecursionContextStates.Set(state.stateNumber);
                }
            }
            // get atn simulator that knows how to do predictions
            Interpreter = new ParserATNSimulator(this, atn);
        }