Ejemplo n.º 1
0
        public virtual IntervalSet GetSetFromCharSetLiteral(GrammarAST charSetAST)
        {
            string chars = charSetAST.Text;

            chars = chars.Substring(1, chars.Length - 2);
            string      cset = '"' + chars + '"';
            IntervalSet set  = new IntervalSet();

            if (chars.Length == 0)
            {
                g.tool.errMgr.GrammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED,
                                           g.fileName, charSetAST.Token, "[]");
                return(set);
            }

            // unescape all valid escape char like \n, leaving escaped dashes as '\-'
            // so we can avoid seeing them as '-' range ops.
            chars = CharSupport.GetStringFromGrammarStringLiteral(cset);
            if (chars == null)
            {
                g.tool.errMgr.GrammarError(ErrorType.INVALID_ESCAPE_SEQUENCE,
                                           g.fileName, charSetAST.Token);
                return(set);
            }
            int n = chars.Length;

            // now make x-y become set of char
            for (int i = 0; i < n; i++)
            {
                int c = chars[i];
                if (c == '\\' && (i + 1) < n && chars[i + 1] == '-')
                { // \-
                    CheckSetCollision(charSetAST, set, '-');
                    set.Add('-');
                    i++;
                }
                else if ((i + 2) < n && chars[i + 1] == '-')
                { // range x-y
                    int x = c;
                    int y = chars[i + 2];
                    if (x <= y)
                    {
                        CheckSetCollision(charSetAST, set, x, y);
                        set.Add(x, y);
                    }
                    else
                    {
                        g.tool.errMgr.GrammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED,
                                                   g.fileName, charSetAST.Token, "[" + (char)x + "-" + (char)y + "]");
                    }
                    i += 2;
                }
                else
                {
                    CheckSetCollision(charSetAST, set, c);
                    set.Add(c);
                }
            }
            return(set);
        }
Ejemplo n.º 2
0
        public virtual string GetOptionString(string key)
        {
            GrammarAST value = GetOptionAST(key);

            if (value == null)
            {
                return(null);
            }
            if (value is ActionAST)
            {
                return(value.Text);
            }
            else
            {
                string v = value.Text;
                if (v.StartsWith("'") || v.StartsWith("\""))
                {
                    v = CharSupport.GetStringFromGrammarStringLiteral(v);
                    if (v == null)
                    {
                        g.tool.errMgr.GrammarError(ErrorType.INVALID_ESCAPE_SEQUENCE, g.fileName, value.Token);
                        v = "";
                    }
                }
                return(v);
            }
        }
Ejemplo n.º 3
0
        /** For a lexer, a string is a sequence of char to match.  That is,
         *  "fog" is treated as 'f' 'o' 'g' not as a single transition in
         *  the DFA.  Machine== o-'f'-&gt;o-'o'-&gt;o-'g'-&gt;o and has n+1 states
         *  for n characters.
         */
        public override Handle StringLiteral(TerminalAST stringLiteralAST)
        {
            string   chars = stringLiteralAST.Text;
            ATNState left  = NewState(stringLiteralAST);
            ATNState right;

            chars = CharSupport.GetStringFromGrammarStringLiteral(chars);
            if (chars == null)
            {
                g.tool.errMgr.GrammarError(ErrorType.INVALID_ESCAPE_SEQUENCE,
                                           g.fileName, stringLiteralAST.Token);
                return(new Handle(left, left));
            }

            int      n    = chars.Length;
            ATNState prev = left;

            right = null;
            for (int i = 0; i < n; i++)
            {
                right = NewState(stringLiteralAST);
                prev.AddTransition(new AtomTransition(right, chars[i]));
                prev = right;
            }
            stringLiteralAST.atnState = left;
            return(new Handle(left, right));
        }
Ejemplo n.º 4
0
        public override Handle LexerCommand(GrammarAST ID)
        {
            ILexerAction lexerAction = CreateLexerAction(ID, null);

            if (lexerAction != null)
            {
                return(Action(ID, lexerAction));
            }

            if (codegenTemplates == null)
            {
                // suppress reporting a single missing template when the target couldn't be loaded
                return(Epsilon(ID));
            }

            // fall back to standard action generation for the command
            Template cmdST = codegenTemplates.GetInstanceOf("Lexer" +
                                                            CharSupport.Capitalize(ID.Text) +
                                                            "Command");

            if (cmdST == null)
            {
                g.tool.errMgr.GrammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.Token, ID.Text);
                return(Epsilon(ID));
            }

            if (cmdST.impl.FormalArguments != null && cmdST.impl.FormalArguments.Any(x => x.Name == "arg"))
            {
                g.tool.errMgr.GrammarError(ErrorType.MISSING_LEXER_COMMAND_ARGUMENT, g.fileName, ID.Token, ID.Text);
                return(Epsilon(ID));
            }

            return(Action(cmdST.Render()));
        }
Ejemplo n.º 5
0
        public override Handle Range(GrammarAST a, GrammarAST b)
        {
            ATNState left  = NewState(a);
            ATNState right = NewState(b);
            int      t1    = CharSupport.GetCharValueFromGrammarCharLiteral(a.Text);
            int      t2    = CharSupport.GetCharValueFromGrammarCharLiteral(b.Text);

            CheckRange(a, b, t1, t2);
            left.AddTransition(new RangeTransition(right, t1, t2));
            a.atnState = left;
            b.atnState = left;
            return(new Handle(left, right));
        }
Ejemplo n.º 6
0
        protected virtual int GetTokenType([NotNull] GrammarAST atom)
        {
            int ttype;

            if (g.IsLexer())
            {
                ttype = CharSupport.GetCharValueFromGrammarCharLiteral(atom.Text);
            }
            else
            {
                ttype = g.GetTokenType(atom.Text);
            }
            return(ttype);
        }
Ejemplo n.º 7
0
        public override Handle Set(GrammarAST associatedAST, IList <GrammarAST> alts, bool invert)
        {
            ATNState    left  = NewState(associatedAST);
            ATNState    right = NewState(associatedAST);
            IntervalSet set   = new IntervalSet();

            foreach (GrammarAST t in alts)
            {
                if (t.Type == ANTLRParser.RANGE)
                {
                    int a = CharSupport.GetCharValueFromGrammarCharLiteral(t.GetChild(0).Text);
                    int b = CharSupport.GetCharValueFromGrammarCharLiteral(t.GetChild(1).Text);
                    if (CheckRange((GrammarAST)t.GetChild(0), (GrammarAST)t.GetChild(1), a, b))
                    {
                        CheckSetCollision(associatedAST, set, a, b);
                        set.Add(a, b);
                    }
                }
                else if (t.Type == ANTLRParser.LEXER_CHAR_SET)
                {
                    set.AddAll(GetSetFromCharSetLiteral(t));
                }
                else if (t.Type == ANTLRParser.STRING_LITERAL)
                {
                    int c = CharSupport.GetCharValueFromGrammarCharLiteral(t.Text);
                    if (c != -1)
                    {
                        CheckSetCollision(associatedAST, set, c);
                        set.Add(c);
                    }
                    else
                    {
                        g.tool.errMgr.GrammarError(ErrorType.INVALID_LITERAL_IN_LEXER_SET,
                                                   g.fileName, t.Token, t.Text);
                    }
                }
                else if (t.Type == ANTLRParser.TOKEN_REF)
                {
                    g.tool.errMgr.GrammarError(ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET,
                                               g.fileName, t.Token, t.Text);
                }
            }
            if (invert)
            {
                left.AddTransition(new NotSetTransition(right, set));
            }
            else
            {
                Transition transition;
                if (set.GetIntervals().Count == 1)
                {
                    Interval interval = set.GetIntervals()[0];
                    transition = new RangeTransition(right, interval.a, interval.b);
                }
                else
                {
                    transition = new SetTransition(right, set);
                }

                left.AddTransition(transition);
            }
            associatedAST.atnState = left;
            return(new Handle(left, right));
        }