Antlr4.Automata.LexerATNFactory.GetSetFromCharSetLiteral C# (CSharp) Method

GetSetFromCharSetLiteral() public method

public GetSetFromCharSetLiteral ( GrammarAST charSetAST ) : Antlr4.Runtime.Misc.IntervalSet
charSetAST Antlr4.Tool.Ast.GrammarAST
return Antlr4.Runtime.Misc.IntervalSet
        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;
        }