Exemplo n.º 1
0
 public static Guid ToUUID(char[] data, int offset)
 {
     return(ATNDeserializer.ToUUID(data, offset));
 }
Exemplo n.º 2
0
 public static int ToInt32(char[] data, int offset)
 {
     return(ATNDeserializer.ToInt32(data, offset));
 }
Exemplo n.º 3
0
 public static long ToLong(char[] data, int offset)
 {
     return(ATNDeserializer.ToLong(data, offset));
 }
Exemplo n.º 4
0
        public virtual string Decode(char[] data)
        {
            data = data.Clone();
            // don't adjust the first value since that's the version number
            for (int i = 1; i < data.Length; i++)
            {
                data[i] = (char)(data[i] - 2);
            }
            StringBuilder buf     = new StringBuilder();
            int           p       = 0;
            int           version = ATNDeserializer.ToInt(data[p++]);

            if (version != ATNDeserializer.SerializedVersion)
            {
                string reason = string.Format("Could not deserialize ATN with version {0} (expected {1}).", version, ATNDeserializer.SerializedVersion);
                throw new NotSupportedException(new InvalidClassException(typeof(ATN).FullName, reason));
            }
            UUID uuid = ATNDeserializer.ToUUID(data, p);

            p += 8;
            if (!uuid.Equals(ATNDeserializer.SerializedUuid))
            {
                string reason = string.Format(CultureInfo.CurrentCulture, "Could not deserialize ATN with UUID {0} (expected {1}).", uuid, ATNDeserializer.SerializedUuid);
                throw new NotSupportedException(new InvalidClassException(typeof(ATN).FullName, reason));
            }
            p++;
            // skip grammarType
            int maxType = ATNDeserializer.ToInt(data[p++]);

            buf.Append("max type ").Append(maxType).Append("\n");
            int nstates = ATNDeserializer.ToInt(data[p++]);

            for (int i_1 = 0; i_1 < nstates; i_1++)
            {
                StateType stype = StateType.Values()[ATNDeserializer.ToInt(data[p++])];
                if (stype == StateType.InvalidType)
                {
                    continue;
                }
                // ignore bad type of states
                int ruleIndex = ATNDeserializer.ToInt(data[p++]);
                if (ruleIndex == char.MaxValue)
                {
                    ruleIndex = -1;
                }
                string arg = string.Empty;
                if (stype == StateType.LoopEnd)
                {
                    int loopBackStateNumber = ATNDeserializer.ToInt(data[p++]);
                    arg = " " + loopBackStateNumber;
                }
                else
                {
                    if (stype == StateType.PlusBlockStart || stype == StateType.StarBlockStart || stype == StateType.BlockStart)
                    {
                        int endStateNumber = ATNDeserializer.ToInt(data[p++]);
                        arg = " " + endStateNumber;
                    }
                }
                buf.Append(i_1).Append(":").Append(ATNState.serializationNames[(int)(stype)]).Append(" ").Append(ruleIndex).Append(arg).Append("\n");
            }
            int numNonGreedyStates = ATNDeserializer.ToInt(data[p++]);

            for (int i_2 = 0; i_2 < numNonGreedyStates; i_2++)
            {
                int stateNumber = ATNDeserializer.ToInt(data[p++]);
            }
            int numSllStates = ATNDeserializer.ToInt(data[p++]);

            for (int i_3 = 0; i_3 < numSllStates; i_3++)
            {
                int stateNumber = ATNDeserializer.ToInt(data[p++]);
            }
            int numPrecedenceStates = ATNDeserializer.ToInt(data[p++]);

            for (int i_4 = 0; i_4 < numPrecedenceStates; i_4++)
            {
                int stateNumber = ATNDeserializer.ToInt(data[p++]);
            }
            int nrules = ATNDeserializer.ToInt(data[p++]);

            for (int i_5 = 0; i_5 < nrules; i_5++)
            {
                int  s            = ATNDeserializer.ToInt(data[p++]);
                bool leftFactored = ATNDeserializer.ToInt(data[p++]) != 0;
                if (atn.grammarType == ATNType.Lexer)
                {
                    int arg1 = ATNDeserializer.ToInt(data[p++]);
                    buf.Append("rule ").Append(i_5).Append(":").Append(s).Append(" ").Append(arg1).Append('\n');
                }
                else
                {
                    buf.Append("rule ").Append(i_5).Append(":").Append(s).Append('\n');
                }
            }
            int nmodes = ATNDeserializer.ToInt(data[p++]);

            for (int i_6 = 0; i_6 < nmodes; i_6++)
            {
                int s = ATNDeserializer.ToInt(data[p++]);
                buf.Append("mode ").Append(i_6).Append(":").Append(s).Append('\n');
            }
            int nsets = ATNDeserializer.ToInt(data[p++]);

            for (int i_7 = 0; i_7 < nsets; i_7++)
            {
                int nintervals = ATNDeserializer.ToInt(data[p++]);
                buf.Append(i_7).Append(":");
                bool containsEof = data[p++] != 0;
                if (containsEof)
                {
                    buf.Append(GetTokenName(TokenConstants.Eof));
                }
                for (int j = 0; j < nintervals; j++)
                {
                    if (containsEof || j > 0)
                    {
                        buf.Append(", ");
                    }
                    buf.Append(GetTokenName(ATNDeserializer.ToInt(data[p]))).Append("..").Append(GetTokenName(ATNDeserializer.ToInt(data[p + 1])));
                    p += 2;
                }
                buf.Append("\n");
            }
            int nedges = ATNDeserializer.ToInt(data[p++]);

            for (int i_8 = 0; i_8 < nedges; i_8++)
            {
                int src   = ATNDeserializer.ToInt(data[p]);
                int trg   = ATNDeserializer.ToInt(data[p + 1]);
                int ttype = ATNDeserializer.ToInt(data[p + 2]);
                int arg1  = ATNDeserializer.ToInt(data[p + 3]);
                int arg2  = ATNDeserializer.ToInt(data[p + 4]);
                int arg3  = ATNDeserializer.ToInt(data[p + 5]);
                buf.Append(src).Append("->").Append(trg).Append(" ").Append(Transition.serializationNames[ttype]).Append(" ").Append(arg1).Append(",").Append(arg2).Append(",").Append(arg3).Append("\n");
                p += 6;
            }
            int ndecisions = ATNDeserializer.ToInt(data[p++]);

            for (int i_9 = 0; i_9 < ndecisions; i_9++)
            {
                int s = ATNDeserializer.ToInt(data[p++]);
                buf.Append(i_9).Append(":").Append(s).Append("\n");
            }
            if (atn.grammarType == ATNType.Lexer)
            {
                int lexerActionCount = ATNDeserializer.ToInt(data[p++]);
                for (int i_10 = 0; i_10 < lexerActionCount; i_10++)
                {
                    LexerActionType actionType = LexerActionType.Values()[ATNDeserializer.ToInt(data[p++])];
                    int             data1      = ATNDeserializer.ToInt(data[p++]);
                    int             data2      = ATNDeserializer.ToInt(data[p++]);
                }
            }
            return(buf.ToString());
        }
Exemplo n.º 5
0
 public static int ToInt(char c)
 {
     return(ATNDeserializer.ToInt(c));
 }
        private RuleRelations ExtractRuleRelations(INamedTypeSymbol recognizerType)
        {
            string serializedATN = GetSerializedATN(recognizerType);
            if (serializedATN == null)
                return null;

            ATN atn = new ATNDeserializer().Deserialize(serializedATN.ToCharArray());
            RuleRelations relations = new RuleRelations(atn.ruleToStartState.Length);
            foreach (ATNState state in atn.states)
            {
                if (!state.epsilonOnlyTransitions)
                    continue;

                foreach (Transition transition in state.Transitions)
                {
                    RuleTransition ruleTransition = transition as RuleTransition;
                    if (ruleTransition == null)
                        continue;

                    relations.AddRuleInvocation(state.ruleIndex, ruleTransition.target.ruleIndex);
                }
            }

            return relations;
        }
Exemplo n.º 7
0
 private static RuleDependencyChecker.RuleRelations ExtractRuleRelations(Type recognizer)
 {
     string serializedATN = GetSerializedATN(recognizer);
     if (serializedATN == null)
     {
         return null;
     }
     ATN atn = new ATNDeserializer().Deserialize(serializedATN.ToCharArray());
     RuleDependencyChecker.RuleRelations relations = new RuleDependencyChecker.RuleRelations(atn.ruleToStartState.Length);
     foreach (ATNState state in atn.states)
     {
         if (!state.epsilonOnlyTransitions)
         {
             continue;
         }
         foreach (Transition transition in state.Transitions)
         {
             if (transition.TransitionType != TransitionType.Rule)
             {
                 continue;
             }
             RuleTransition ruleTransition = (RuleTransition)transition;
             relations.AddRuleInvocation(state.ruleIndex, ruleTransition.target.ruleIndex);
         }
     }
     return relations;
 }
Exemplo n.º 8
0
        /** Derive a new parser from an old one that has knowledge of the grammar.
         *  The Grammar object is used to correctly compute outer alternative
         *  numbers for parse tree nodes. A parser of the same type is created
         *  for subclasses of {@link ParserInterpreter}.
         */
        public static ParserInterpreter DeriveTempParserInterpreter(Grammar g, Parser originalParser, ITokenStream tokens)
        {
            ParserInterpreter parser;
            if (originalParser is ParserInterpreter)
            {
                Type c = originalParser.GetType();
                try
                {
                    parser = (ParserInterpreter)Activator.CreateInstance(originalParser.GetType(), g, originalParser.Atn, originalParser.InputStream);
                }
                catch (Exception e)
                {
                    throw new ArgumentException("can't create parser to match incoming " + c.Name, e);
                }
            }
            else
            {
                // must've been a generated parser
                char[] serializedAtn = ATNSerializer.GetSerializedAsChars(originalParser.Atn, originalParser.RuleNames);
                ATN deserialized = new ATNDeserializer().Deserialize(serializedAtn);
                parser = new ParserInterpreter(originalParser.GrammarFileName,
                                               originalParser.Vocabulary,
                                               originalParser.RuleNames,
                                               deserialized,
                                               tokens);
            }

            parser.SetInputStream(tokens);

            // Make sure that we don't get any error messages from using this temporary parser
            parser.ErrorHandler = new BailErrorStrategy();
            parser.RemoveErrorListeners();
            parser.RemoveParseListeners();
            parser.Interpreter.PredictionMode = PredictionMode.LlExactAmbigDetection;
            return parser;
        }
Exemplo n.º 9
0
        public virtual ParserInterpreter CreateParserInterpreter(ITokenStream tokenStream)
        {
            if (this.IsLexer())
            {
                throw new InvalidOperationException("A parser interpreter can only be created for a parser or combined grammar.");
            }

            char[] serializedAtn = ATNSerializer.GetSerializedAsChars(atn, GetRuleNames());
            ATN deserialized = new ATNDeserializer().Deserialize(serializedAtn);
            return new ParserInterpreter(fileName, GetVocabulary(), GetRuleNames(), deserialized, tokenStream);
        }
Exemplo n.º 10
0
        public virtual LexerInterpreter CreateLexerInterpreter(ICharStream input)
        {
            if (this.IsParser())
            {
                throw new InvalidOperationException("A lexer interpreter can only be created for a lexer or combined grammar.");
            }

            if (this.IsCombined())
            {
                return implicitLexer.CreateLexerInterpreter(input);
            }

            char[] serializedAtn = ATNSerializer.GetSerializedAsChars(atn, GetRuleNames());
            ATN deserialized = new ATNDeserializer().Deserialize(serializedAtn);
            return new LexerInterpreter(fileName, GetVocabulary(), GetRuleNames(), ((LexerGrammar)this).modes.Keys, deserialized, input);
        }