コード例 #1
0
        public LsmStatePathBuilder(LsmDocument document, LsmState rootState)
        {
            if (rootState == null)
                throw new ArgumentNullException("rootState");

            if (document == null)
                throw new ArgumentNullException("document");

            _document = document;
            _rootState = rootState;
            _lastState = rootState;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: gaxar77/RuntimeParseLib
        static void Main(string[] args)
        {
            /*
            LsmBuilder builder = new LsmBuilder();

            builder.CreateStringTokenPath("H", 1, true);
            builder.CreateStringTokenPath("He", 2, true);
            builder.CreateStringTokenPath("Heck", 3, true);
            builder.CreateStringTokenPath("Hello", 4, true);
            builder.CreateStringTokenPath("{", 5, false);
            builder.CreateStringTokenPath("}", 6, false);
            builder.ApplyIdentifierPaths(-1);
            */
            /*
            LsmDocument stateDoc = new LsmDocument();
            LsmState tokenStartState = new LsmState();
            LsmState consumeLetters = new LsmState();

            tokenStartState.DefaultActions.Add(new LsmMarkTokenStartAction());
            tokenStartState.DefaultActions.Add(new LsmStateTransitionAction(consumeLetters));
            tokenStartState.DefaultActions.Add(new LsmClearTokenTextAction());
            consumeLetters.PostStreamActions.Add(new LsmAcceptTokenAction(0));

            LsmLambdaMatchRule letterRule = new LsmLambdaMatchRule((c) => char.IsLetter(c));
            letterRule.Actions.Add(new LsmAdvanceAction());
            letterRule.Actions.Add(new LsmAcceptCharAction());

            consumeLetters.DefaultActions.Add(new LsmAcceptTokenAction(0));
            consumeLetters.DefaultActions.Add(new LsmStateTransitionAction(tokenStartState));
            consumeLetters.DefaultActions.Add(new LsmAdvanceAction());

            consumeLetters.MatchRules.Add(letterRule);
            stateDoc.States.Add(tokenStartState);
            stateDoc.States.Add(consumeLetters);
            stateDoc.StartState = tokenStartState;
            */
            /*
            LsmDocument doc = new LsmDocument();
            LsmState st = new LsmState();
            LsmMatchRule alphaNum = new LsmClassMatchRule(LsmCharacterClass.Alphanumeric);
            alphaNum.Actions.Add(new LsmAcceptCharAction());
            alphaNum.Actions.Add(new LsmAdvanceAction());
            LsmMatchRule thirdChar = new LsmCharIndexMatchRule(2);
            thirdChar.Actions.Add(new LsmAcceptTokenAction(10));
            thirdChar.Actions.Add(new LsmAdvanceAction());
            thirdChar.Actions.Add(new LsmClearTokenTextAction());

            st.MatchRules.Add(alphaNum);
            st.MatchRules.Add(thirdChar);
            doc.States.Add(st);
            doc.StartState = st;
            */
            LsmDocument doc = new LsmDocument();
            LsmState rootState = new LsmState();
            LsmStatePathBuilder path = new LsmStatePathBuilder(doc, rootState);
            path.AppendText("Cat");
            path.CompleteToken(1);
            path.MoveToState(rootState);
            path.AppendText("Dog");
            path.CompleteToken(2);
            path.MoveToState(rootState);
            path.AppendText("Dog");
            path.AppendCharacterClass(LsmCharacterClass.Alphanumeric);
            path.AppendCharacterClassLoop(LsmCharacterClass.Alphanumeric);
            path.CompleteToken(-1);
            path.MoveToState(rootState);

            //Note: Does not loop.
            path.AppendCharacterClass(LsmCharacterClass.WhiteSpace);

            doc.States.Add(rootState);
            doc.StartState = rootState;

            using (TextWriter writer = File.CreateText(@"C:\Users\Guido\Desktop\LSMSource.txt"))
            {
                LsmDocumentTextGenerator gen = new LsmDocumentTextGenerator(writer);
                gen.WriteDocument(doc);
                writer.Flush();
            }

            LsmTokenizeFunction parseFunc
                = doc.BuildLexerFunction();

            List<LsmToken> tokens;

            while (true)
            {
                string stuff = Console.ReadLine();
                using (StringReader reader = new StringReader(stuff))
                    parseFunc(reader, tokens = new List<LsmToken>());

                foreach (LsmToken token in tokens)
                {
                    Console.WriteLine(token.Text + " : " + token.TypeID + " : " + token.CharIndex);
                }

            }
        }
コード例 #3
0
 public void WriteDocument(LsmDocument document)
 {
     _writer.WriteLine("Entry State: " + document.StartState.ID);
     foreach (LsmState state in document.States)
         WriteState(state);
 }