示例#1
0
        public void SeparatedLexerAndParserErrors()
        {
            var lexerText  = $@"lexer grammar {TestGrammarName};
                CHAR:   a-z]+;
                DIGIT: [0-9]+;
                WS:    [ \r\n\t]+ -> skip;";
            var parserText = $@"parser grammar {TestGrammarName};
                start: DIGIT+;
                #";
            var workflow   = new Workflow(GrammarFactory.CreateDefaultSeparatedAndFill(lexerText, parserText, TestGrammarName, "."));

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.GrammarChecked, state.Stage, state.Exception?.ToString());

            var testLexerSource  = new CodeSource(TestGrammarName + "Lexer.g4", File.ReadAllText(TestGrammarName + "Lexer.g4"));
            var testParserSource = new CodeSource(TestGrammarName + "Parser.g4", File.ReadAllText(TestGrammarName + "Parser.g4"));
            GrammarCheckedState grammarCheckedState = state as GrammarCheckedState;

            CollectionAssert.AreEquivalent(
                new [] {
                new ParsingError(2, 25, $"error: {TestGrammarName}Lexer.g4:2:25: token recognition error at: '-z'", testLexerSource, WorkflowStage.GrammarChecked),
                new ParsingError(2, 27, $"error: {TestGrammarName}Lexer.g4:2:27: token recognition error at: ']'", testLexerSource, WorkflowStage.GrammarChecked),
                new ParsingError(2, 28, $"error: {TestGrammarName}Lexer.g4:2:28: mismatched input '+' expecting {{ASSIGN, PLUS_ASSIGN}}", testLexerSource, WorkflowStage.GrammarChecked),
                new ParsingError(3, 16, $"error: {TestGrammarName}Parser.g4:3:16: extraneous input '#' expecting {{<EOF>, 'mode'}}", testParserSource, WorkflowStage.GrammarChecked)
            },
                grammarCheckedState.Errors);
        }
示例#2
0
        public void ParserGeneratedStageErrors()
        {
            var grammarText =
                $@"grammar {TestGrammarName};
                start:  rule1+;
                rule:   DIGIT;
                CHAR:   [a-z]+;
                DIGIT:  [0-9]+;
                WS:     [ \r\n\t]+ -> skip;";
            var workflow = new Workflow(GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, "."));

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.ParserGenerated, state.Stage, state.Exception?.ToString());

            var    grammarSource       = new CodeSource(TestGrammarName + ".g4", File.ReadAllText(TestGrammarName + ".g4"));
            char   separator           = Path.DirectorySeparatorChar;
            string testGrammarFullName = $"{Environment.CurrentDirectory}{separator}.{separator}{TestGrammarName}.g4";
            ParserGeneratedState parserGeneratedState = state as ParserGeneratedState;
            string str = new ParsingError(2, 24, $"error(56): {testGrammarFullName}:2:24: reference to undefined rule: rule1", grammarSource, WorkflowStage.ParserGenerated).ToString();

            CollectionAssert.AreEquivalent(
                new [] {
                new ParsingError(2, 24, $"error(56): {testGrammarFullName}:2:24: reference to undefined rule: rule1", grammarSource, WorkflowStage.ParserGenerated),
            },
                parserGeneratedState.Errors);
        }
示例#3
0
        public void TextParsedStageErrors(Runtime runtime)
        {
            var grammarText =
                $@"grammar {TestGrammarName};
                start: DIGIT+;
                CHAR:  [a-z]+;
                DIGIT: [0-9]+;
                WS:    [ \r\n\t]+ -> skip;";
            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, @"!  asdf  1234");

            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.TextParsed, state.Stage, state.Exception?.ToString());

            TextParsedState textParsedState = state as TextParsedState;
            var             textSource      = textParsedState.Text;

            CollectionAssert.AreEquivalent(
                new [] {
                new ParsingError(1, 1, "line 1:0 token recognition error at: '!'", textSource, WorkflowStage.TextParsed),
                new ParsingError(1, 4, "line 1:3 extraneous input 'asdf' expecting DIGIT", textSource, WorkflowStage.TextParsed)
            },
                textParsedState.Errors);
            Assert.AreEqual("(start asdf 1234)", textParsedState.Tree);
        }
示例#4
0
        public void CheckCustomRoot(Runtime runtime)
        {
            var grammarText =
                $"grammar {TestGrammarName};" +
                "root1: 'V1';" +
                "root2: 'V2';";

            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");
            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            workflow.Root = null;
            File.WriteAllText(TestTextName, "V1");
            var             state           = workflow.Process();
            TextParsedState textParsedState = state as TextParsedState;

            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);

            workflow.Root   = "root1";
            state           = workflow.Process();
            textParsedState = state as TextParsedState;
            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);

            workflow.Root = "root2";
            File.WriteAllText(TestTextName, "V2");
            state           = workflow.Process();
            textParsedState = state as TextParsedState;
            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);
        }
示例#5
0
        public void CheckPackageName(Runtime runtime)
        {
            const string packageName = "TestLanguage";

            var grammarText =
                $@"grammar {TestGrammarName};
                root:  TOKEN;
                TOKEN:  'a';";

            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            grammar.CaseInsensitiveType = CaseInsensitiveType.lower;
            var workflow = new Workflow(grammar)
            {
                Runtime      = runtime,
                PackageName  = packageName,
                TextFileName = TestTextName
            };

            File.WriteAllText(TestTextName, "A");
            var state           = workflow.Process();
            var textParsedState = state as TextParsedState;

            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(textParsedState.HasErrors);
        }
示例#6
0
        public void GrammarGeneratedCodeCorrectMapping(Runtime runtime)
        {
            Assert.Ignore("Not ready");

            var grammarText =
                @"grammar test;
                  rootRule
                      : {a==0}? tokensOrRules* EOF {a++;}
                      ;
                  tokensOrRules
                      : {a==0}? TOKEN+ {a++;}
                      ;
                  TOKEN: {b==0}? [a-z]+ {b++;};
                  DIGIT: {b==0}? [0-9]+ {b++;};";
            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, "test", ".");
            var workflow = new Workflow(grammar);

            workflow.Runtime = runtime;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.ParserCompiled, state.Stage, state.Exception?.ToString());

            ParserCompiledState parserGeneratedState = state as ParserCompiledState;
            var errors = parserGeneratedState.Errors;

            Assert.AreEqual(8, errors.Count);
            Assert.AreEqual(2, errors.Where(e => e.TextSpan.GetLineColumn().BeginLine == 3).Count());
            Assert.AreEqual(2, errors.Where(e => e.TextSpan.GetLineColumn().BeginLine == 6).Count());
            Assert.AreEqual(2, errors.Where(e => e.TextSpan.GetLineColumn().BeginLine == 8).Count());
            Assert.AreEqual(2, errors.Where(e => e.TextSpan.GetLineColumn().BeginLine == 9).Count());
        }
示例#7
0
        public void CheckListenersAndVisitors(Runtime runtime)
        {
            var grammarText =
                $@"grammar {TestGrammarName};
                t: T;
                T: [a-z]+;";
            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, @"asdf");

            var workflow = new Workflow(grammar)
            {
                GenerateListener = true,
                GenerateVisitor  = true
            };

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            var             state           = workflow.Process();
            TextParsedState textParsedState = state as TextParsedState;

            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);

            var allFiles = Directory.GetFiles(Path.Combine(ParserGenerator.HelperDirectoryName, TestGrammarName, runtime.ToString()));

            Assert.IsTrue(allFiles.Any(file => file.Contains("listener", StringComparison.OrdinalIgnoreCase)));
            Assert.IsTrue(allFiles.Any(file => file.Contains("visitor", StringComparison.OrdinalIgnoreCase)));
        }
示例#8
0
        public void AllGeneratorsExists()
        {
            var runtimes    = (Runtime[])Enum.GetValues(typeof(Runtime));
            var grammarText = $@"grammar {TestGrammarName};
                start: DIGIT+;
                CHAR:  [a-z]+;
                DIGIT: [0-9]+;
                WS:    [ \r\n\t]+ -> skip;";
            var workflow    = new Workflow(GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, "."));

            workflow.EndStage = WorkflowStage.ParserGenerated;
            foreach (Runtime runtime in runtimes)
            {
                workflow.Runtime = runtime;
                var state = (ParserGeneratedState)workflow.Process();
                Assert.IsFalse(state.HasErrors, string.Join(Environment.NewLine, state.Errors));

                RuntimeInfo runtimeInfo      = RuntimeInfo.Runtimes[runtime];
                var         extensions       = runtimeInfo.Extensions;
                var         allFiles         = Directory.GetFiles(Path.Combine(ParserGenerator.HelperDirectoryName, TestGrammarName, runtimeInfo.Runtime.ToString()));
                var         actualFilesCount = allFiles.Count(file => extensions.Any(ext => Path.GetExtension(file).EndsWith(ext)));
                Assert.Greater(actualFilesCount, 0, $"Failed to initialize {runtime} runtime");

                foreach (var file in allFiles)
                {
                    File.Delete(file);
                }
            }
        }
示例#9
0
        private static void CheckCaseInsensitiveWorkflow(Runtime runtime, bool lowerCase)
        {
            char c           = lowerCase ? 'a' : 'A';
            var  grammarText =
                $@"grammar {TestGrammarName};
                start:  A A DIGIT;
                A:      '{c}';
                DIGIT:  [0-9]+;
                WS:     [ \r\n\t]+ -> skip;";
            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            grammar.CaseInsensitiveType = lowerCase ? CaseInsensitiveType.lower : CaseInsensitiveType.UPPER;
            File.WriteAllText(TestTextName, @"A a 1234");

            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.TextParsed, state.Stage, state.Exception?.ToString());
            TextParsedState textParsedState = state as TextParsedState;

            Assert.AreEqual(0, textParsedState.Errors.Count, string.Join(Environment.NewLine, textParsedState.Errors));
            Assert.AreEqual("(start A a 1234)", textParsedState.Tree);
        }
示例#10
0
        public Parser(List <Token> tokens, StreamWriter syntaxErrorStream, StreamWriter derivationsStream, StreamWriter astStream)
        {
            _tokens  = tokens;
            _grammar = GrammarFactory.CreateGrammar();

            _lookahead = _tokens[_currentLookaheadIndex];

            _syntaxErrorStream = syntaxErrorStream;
            _derivationsStream = derivationsStream;
            _astStream         = astStream;

            _astBuilder = new ASTBuilder.ASTBuilder();
        }
示例#11
0
        public void CheckPredictionMode(Runtime runtime)
        {
            var grammarText = $@"
grammar {TestGrammarName};

root
    : (stmt1 | stmt2) EOF
    ;
    
stmt1
    : name
    ;

stmt2
    : 'static' name '.' Id
    ;

name
    : Id ('.' Id)*
    ;

Dot        : '.';
Static     : 'static';
Id         : [A-Za-z]+;
Whitespace : [ \t\r\n]+ -> channel(HIDDEN);
";

            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");
            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;
            File.WriteAllText(TestTextName, @"static a.b");

            workflow.PredictionMode = PredictionMode.LL;
            var             llState           = workflow.Process();
            TextParsedState llTextParsedState = llState as TextParsedState;

            Assert.IsNotNull(llTextParsedState);
            Assert.IsFalse(llState.HasErrors);

            workflow.PredictionMode = PredictionMode.SLL;
            var sllState           = workflow.Process();
            var sllTextParsedState = sllState as TextParsedState;

            Assert.IsNotNull(sllTextParsedState);
            Assert.IsTrue(sllTextParsedState.HasErrors);
        }
示例#12
0
    // Use this for initialization
    public void Activate()
    {
        finalGrammar = GrammarFactory.Parse(new GrammarWord(ActionType.Horizontal, 45, 15.0f), recursionDepth);
        float finalDistance = 0;
        float finalTime     = 0;

        foreach (GrammarWord g in finalGrammar)
        {
            if (g.action == ActionType.Horizontal)
            {
                finalDistance += g.amount;
            }
            finalTime += g.duration;
        }

        NextStep();
    }
示例#13
0
        public void DoNotStopProcessingIfWarnings()
        {
            var grammarText =
                $@"grammar {TestGrammarName};
                t: T;
                T:  ['' ]+;";
            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, " ");

            var workflow = new Workflow(grammar);

            workflow.Runtime      = Runtime.Java;
            workflow.TextFileName = TestTextName;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.TextParsed, state.Stage);
            Assert.IsTrue(((TextParsedState)state).ParserCompiledState.ParserGeneratedState.Errors[0].IsWarning);
        }
示例#14
0
        public NewGrammarWindowViewModel(Window window)
        {
            OkCommand = ReactiveCommand.Create(async() =>
            {
                string grammarFileName = Path.Combine(GrammarDirectory, _grammar.Name);
                bool success           = false;

                if (Directory.Exists(grammarFileName))
                {
                    if (await MessageBox.ShowDialog(window, $"Do you want to replace existed grammar {_grammar.Name}?", "", MessageBoxType.YesNo))
                    {
                        success = true;
                    }
                }
                else
                {
                    success = true;
                }

                if (success)
                {
                    GrammarFactory.FillGrammarFiles(_grammar, grammarFileName, true);
                    window.Close(_grammar);
                }
            });

            CancelCommand = ReactiveCommand.Create(() => window.Close(null));

            SelectGrammarDirectory = ReactiveCommand.Create(async() =>
            {
                var openFolderDialog = new OpenFolderDialog
                {
                    InitialDirectory = GrammarDirectory
                };
                var folderName = await openFolderDialog.ShowAsync(window);
                if (!string.IsNullOrEmpty(folderName))
                {
                    GrammarDirectory = folderName;
                }
            });
        }
示例#15
0
        public void ParserCompiledStageErrors(Runtime runtime)
        {
            var grammarText =
                @"grammar Test;
                start:  DIGIT+ {i^;};
                CHAR:   [a-z]+;
                DIGIT:  [0-9]+;
                WS:     [ \r\n\t]+ -> skip;";
            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, "Test", ".");
            var workflow = new Workflow(grammar);

            workflow.Runtime = runtime;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.ParserCompiled, state.Stage, state.Exception?.ToString());

            ParserCompiledState parserGeneratedState = state as ParserCompiledState;

            Assert.GreaterOrEqual(parserGeneratedState.Errors.Count, 1);
            //Assert.AreEqual(runtime == Runtime.Go ? 1 : 2, parserGeneratedState.Errors[0].TextSpan.GetLineColumn().BeginLine);
        }
示例#16
0
        public void CheckLexerOnlyGrammar(Runtime runtime)
        {
            var grammarText =
                $"lexer grammar {TestGrammarName}Lexer;" +
                "T1: 'T1';" +
                "Digit: [0-9]+;" +
                "Space: ' '+ -> channel(HIDDEN);";

            var grammar = GrammarFactory.CreateDefaultLexerAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, "T1 1234");

            var workflow = new Workflow(grammar);

            workflow.Runtime      = runtime;
            workflow.TextFileName = TestTextName;

            var             state           = workflow.Process();
            TextParsedState textParsedState = state as TextParsedState;

            Assert.IsNotNull(textParsedState);
            Assert.IsFalse(state.HasErrors);
        }
示例#17
0
        public void GrammarCheckedStageErrors()
        {
            var grammarText = $@"grammar {TestGrammarName};
                start: DIGIT+;
                CHAR:   a-z]+;
                DIGIT: [0-9]+;
                WS:    [ \r\n\t]+ -> skip;";
            var workflow    = new Workflow(GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, "."));

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.GrammarChecked, state.Stage, state.Exception?.ToString());

            var grammarSource = new CodeSource(TestGrammarName + ".g4", File.ReadAllText(TestGrammarName + ".g4"));
            GrammarCheckedState grammarCheckedState = state as GrammarCheckedState;

            CollectionAssert.AreEquivalent(
                new [] {
                new ParsingError(3, 25, "error: test.g4:3:25: token recognition error at: '-z'", grammarSource, WorkflowStage.GrammarChecked),
                new ParsingError(3, 27, "error: test.g4:3:27: token recognition error at: ']'", grammarSource, WorkflowStage.GrammarChecked),
                new ParsingError(3, 28, "error: test.g4:3:28: mismatched input '+' expecting {ASSIGN, PLUS_ASSIGN}", grammarSource, WorkflowStage.GrammarChecked)
            },
                grammarCheckedState.Errors);
        }
示例#18
0
        public MainWindowViewModel(Window window)
        {
            _window               = window;
            _grammarTextBox       = _window.Find <TextEditor>("GrammarTextBox");
            _textTextBox          = _window.Find <TextEditor>("TextTextBox");
            _grammarErrorsListBox = _window.Find <ListBox>("GrammarErrorsListBox");
            _textErrorsListBox    = _window.Find <ListBox>("TextErrorsListBox");
            _parseTreeTextBox     = _window.Find <TextEditor>("ParseTreeTextBox");
            _tokensTextBox        = _window.Find <TextEditor>("TokensTextBox");

            _grammarTextBox.SetupHightlighting(".g4");

            _settings = Settings.Load();

            _window.WindowState = _settings.WindowState;
            if (_settings.Width > 0)
            {
                _window.Width = _settings.Width;
            }
            if (_settings.Height > 0)
            {
                _window.Height = _settings.Height;
            }
            if (_settings.Left != -1 && _settings.Top != -1)
            {
                _window.Position = new PixelPoint(_settings.Left, _settings.Top);
            }

            Grammar grammar = null;

            bool   openDefaultGrammar = false;
            string packageName        = null;
            string root = null;

            if (string.IsNullOrEmpty(_settings.GrammarFileOrDirectory))
            {
                openDefaultGrammar = true;
            }
            else
            {
                try
                {
                    grammar = GrammarFactory.Open(_settings.GrammarFileOrDirectory, out packageName, out root);
                }
                catch (Exception ex)
                {
                    ShowOpenFileErrorMessage(_settings.GrammarFileOrDirectory, ex.Message);

                    _settings.OpenedGrammarFile = "";
                    openDefaultGrammar          = true;
                }
            }

            if (openDefaultGrammar)
            {
                grammar = GrammarFactory.CreateDefault();
                GrammarFactory.FillGrammarFiles(grammar, Settings.Directory, false);
                _settings.GrammarFileOrDirectory = grammar.Directory;
                _settings.Save();
            }

            _workflow       = new Workflow(grammar);
            SelectedRuntime = RuntimeInfo.InitOrGetRuntimeInfo(_workflow.Runtime);
            PackageName     = packageName;
            Root            = root;

            InitFiles();
            if (string.IsNullOrEmpty(_settings.OpenedGrammarFile) || !grammar.Files.Contains(_settings.OpenedGrammarFile))
            {
                OpenedGrammarFile = GrammarFiles.FirstOrDefault();
            }
            else
            {
                OpenedGrammarFile = _settings.OpenedGrammarFile;
            }

            if (string.IsNullOrEmpty(_settings.OpenedTextFile))
            {
                OpenedTextFile = TextFiles.Count > 0 ? TextFiles.First() : null;
            }
            else
            {
                OpenedTextFile = new FileName(_settings.OpenedTextFile);
            }

            SetupWindowSubscriptions();
            SetupWorkflowSubscriptions();
            SetupTextBoxSubscriptions();
            SetupCommandSubscriptions();

            AutoProcessing = _settings.Autoprocessing;
        }
示例#19
0
        public void CheckIncorrectGrammarDefinedOptions()
        {
            var grammarText =
                @$ "grammar {TestGrammarName};
// caseInsensitiveType=incorrect;
// language=incorrect;
// package=incorrect;
// visitor=incorrect;
// listener=incorrect;
// root=incorrect;
// predictionMode=incorrect;

// caseInsensitiveType=lower;
// language=JavaScript;
// package=package;
// visitor=true;
// listener=true;
// root=root;
// predictionMode=sll;

root:
    .*? ;

TOKEN: 'token';";

            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");
            var workflow = new Workflow(grammar);

            workflow.TextFileName = TestTextName;
            workflow.EndStage     = WorkflowStage.GrammarChecked;
            var state = (GrammarCheckedState)workflow.Process();

            Assert.AreEqual(CaseInsensitiveType.lower, state.CaseInsensitiveType);
            Assert.AreEqual(Runtime.JavaScript, state.Runtime);
            Assert.AreEqual("package", state.Package);
            Assert.AreEqual(true, state.Listener);
            Assert.AreEqual(true, state.Visitor);
            Assert.AreEqual("root", state.Root);
            Assert.AreEqual(PredictionMode.SLL, state.PredictionMode);

            CheckIncorrect("caseInsensitiveType");
            CheckIncorrect("language");
            CheckIncorrect("package", true);
            CheckIncorrect("visitor");
            CheckIncorrect("listener");
            CheckIncorrect("root");
            CheckIncorrect("predictionMode");

            void CheckIncorrect(string optionName, bool notError = false)
            {
                bool Checker(ParsingError error) => error.Message.Contains(optionName != "root"
                    ? $"Incorrect option {optionName}"
                    : "Root incorrect is not exist");

                if (!notError)
                {
                    Assert.IsTrue(state.Errors.Any(Checker));
                }
                else
                {
                    Assert.IsFalse(state.Errors.Any(Checker));
                }
            }

            CheckDuplication("caseInsensitiveType");
            CheckDuplication("language");
            CheckDuplication("package");
            CheckDuplication("visitor");
            CheckDuplication("listener");
            CheckDuplication("root");
            CheckDuplication("predictionMode");

            void CheckDuplication(string optionName)
            {
                Assert.IsTrue(state.Errors.Any(error => error.Message.Contains($"Option {optionName} is already defined")));
            }
        }
示例#20
0
        public void TextParsedStageErrors(Runtime runtime)
        {
            var grammarText =
                $@"grammar {TestGrammarName};

root
    : missingToken extraneousToken noViableAlternative mismatchedInput EOF
    ;

missingToken
    : Error LParen RParen Semi
    ;

extraneousToken
    : Error Id Semi
    ;

mismatchedInput
    : Error Id Semi
    ;

noViableAlternative
    : AA BB
    | AA CC
    ;
    
AA: 'aa';
BB: 'bb';
CC: 'cc';
DD: 'dd';
LParen     : '((';
RParen     : '))';
Semi       : ';';
Error      : 'error';
Id         : [A-Za-z][A-Za-z0-9]+;
Whitespace : [ \t\r\n]+ -> channel(HIDDEN);
Comment    : '//' ~[\r\n]* -> channel(HIDDEN);
Number     : [0-9']+;";

            var grammar = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, TestGrammarName, ".");

            File.WriteAllText(TestTextName, @"#                       // token recognition error at: '#'
error (( ;        // missing '))' at ';'
error id1 id2 ;   // extraneous input 'id2' expecting ';'
aa  dd            // no viable alternative at input 'aa  dd'
error 123 456 ;   // mismatched input '123' expecting Id");

            var workflow = new Workflow(grammar)
            {
                Runtime = runtime, TextFileName = TestTextName
            };

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.TextParsed, state.Stage, state.Exception?.ToString());

            TextParsedState textParsedState = (TextParsedState)state;
            var             textSource      = textParsedState.Text;

            CollectionAssert.AreEquivalent(
                new [] {
                new ParsingError(1, 1, 1, 2, "line 1:0 token recognition error at: '#'", textSource, WorkflowStage.TextParsed),
                new ParsingError(2, 10, 2, 11, "line 2:9 missing '))' at ';'", textSource, WorkflowStage.TextParsed),
                new ParsingError(3, 11, 3, 14, "line 3:10 extraneous input 'id2' expecting ';'", textSource, WorkflowStage.TextParsed),
                new ParsingError(4, 5, 4, 7, "line 4:4 no viable alternative at input 'aa  dd'", textSource, WorkflowStage.TextParsed),
                new ParsingError(5, 7, 5, 10, "line 5:6 mismatched input '123' expecting Id", textSource, WorkflowStage.TextParsed)
            },
                textParsedState.Errors);

            // TODO: unify in different runtimes
            //Assert.AreEqual("(root (missingToken error (( <missing '))'> ;) (extraneousToken error id1 id2 ;) (noViableAlternative aa dd) (mismatchedInput error 123 456 ;) EOF)", textParsedState.Tree);
        }
示例#21
0
    public static List <GrammarWord> ApplyGrammar(GrammarWord source)
    {
        List <GrammarWord> result = new List <GrammarWord>();

        switch (source.action)
        {
        case ActionType.InPlaceTBD:
        {
            int choice = Random.Range(0, 3);
            if (choice == 0)
            {
                //Pause
                result.Add(new GrammarWord(ActionType.Wait, 0, source.duration));
            }
            else if (choice == 1)
            {
                //360 spin
                result.Add(new GrammarWord(ActionType.Rotate, 360.0f, source.duration));
            }
            else if (choice == 2)
            {
                //Shift vertical
                result.Add(new GrammarWord(ActionType.Vertical, Random.Range(-8, 8), source.duration));
            }
        }
        break;

        case ActionType.Horizontal:
        {
            int choice = Random.Range(0, 4);
            //Forward, turn, back, turn, forward
            if (choice == 0)
            {
                //Caclulate proportion of time and distance for each forward segment
                float t1 = 0.3f;                                //Random.Range(0.2f, 0.6f);
                float t3 = 0.2f;                                //Random.Range(0.15f, 1.0f - t1 - 0.2f);
                float t5 = 1.0f - t1 - t3;

                //Caclulate proportion of time for Each rotation
                float t2 = Random.Range(0.2f, 0.7f);
                float t4 = 1.0f - t2;

                //Forward segment t1
                result.Add(new GrammarWord(ActionType.Horizontal, source.amount * t1, source.duration * 0.7f * t1));
                //Spin segment t2
                result.Add(new GrammarWord(ActionType.Rotate, 180.0f, source.duration * 0.3f * t2));
                //Backward segment t3
                result.Add(new GrammarWord(ActionType.Horizontal, (-1.0f) * source.amount * t3, source.duration * 0.7f * t3));
                //Spin segment t4
                result.Add(new GrammarWord(ActionType.Rotate, -180.0f, source.duration * 0.3f * t4));
                //Forward segment t5
                result.Add(new GrammarWord(ActionType.Horizontal, source.amount * (1.0f - t1 + t3), source.duration * 0.7f * t5));
            }
            else if (choice == 1 || choice == 3)
            {
                //forward t1, tbd t2, forward t3
                float t1 = Random.Range(0.2f, 0.6f);
                float t2 = 1.0f;
                float t3 = 1.0f - t1;

                //Forward segment t1
                result.Add(new GrammarWord(ActionType.Horizontal, source.amount * t1, source.duration * 0.75f * t1));
                //TBD segment t2
                result.AddRange(GrammarFactory.ApplyGrammar(new GrammarWord(ActionType.InPlaceTBD, -1.0f, source.duration * t2 * 0.25f)));
                //result.Add(new GrammarWord(ActionType.InPlaceTBD, -1.0f, source.duration * t2 * 0.5f));
                //Forward segment t3
                result.Add(new GrammarWord(ActionType.Horizontal, source.amount * t3, source.duration * 0.75f * t3));
            }
            else if (choice == 2)
            {
                //forward, tbd, back, tbd, forward

                //Caclulate proportion of time and distance for each forward segment
                float t1 = 0.3f;                                //Random.Range(0.2f, 0.6f);
                float t3 = 0.2f;                                //Random.Range(0.15f, 1.0f - t1 - 0.2f);
                float t5 = 1.0f - t1 - t3;

                //Caclulate proportion of time for Each rotation
                float t2 = Random.Range(0.2f, 0.7f);
                float t4 = 1.0f - t2;

                //Forward segment t1
                result.Add(new GrammarWord(ActionType.Horizontal, source.amount * t1, source.duration * 0.7f * t1));
                //TBD segment t2
                result.AddRange(GrammarFactory.ApplyGrammar(new GrammarWord(ActionType.InPlaceTBD, -1, source.duration * 0.3f * t2)));
                //result.Add(new GrammarWord(ActionType.InPlaceTBD, -1, source.duration * 0.3f * t2));
                //Backward segment t3
                result.Add(new GrammarWord(ActionType.Horizontal, (-1.0f) * source.amount * t3, source.duration * 0.7f * t3));
                //TBD segment t4
                result.AddRange(GrammarFactory.ApplyGrammar(new GrammarWord(ActionType.InPlaceTBD, -1, source.duration * 0.3f * t4)));
                //result.Add(new GrammarWord(ActionType.InPlaceTBD, -1 , source.duration * 0.3f * t4));
                //Forward segment t5
                result.Add(new GrammarWord(ActionType.Horizontal, source.amount * (1.0f - t1 + t3), source.duration * 0.7f * t5));
            }
        }
        break;

        default:
            result.Add(source);
            break;
        }
        return(result);
    }
示例#22
0
        public MainWindowViewModel(Window window)
        {
            _window               = window;
            _grammarTextBox       = _window.Find <TextEditor>("GrammarTextBox");
            _textTextBox          = _window.Find <TextEditor>("TextTextBox");
            _grammarErrorsListBox = _window.Find <ListBox>("GrammarErrorsListBox");
            _textErrorsListBox    = _window.Find <ListBox>("TextErrorsListBox");
            _parseTreeTextBox     = _window.Find <TextEditor>("ParseTreeTextBox");
            _tokensTextBox        = _window.Find <TextEditor>("TokensTextBox");

            _grammarTextBox.SetupHightlighting(".g4");

            _settings = Settings.Load();

            _window.WindowState = _settings.WindowState;
            if (_settings.Width > 0)
            {
                _window.Width = _settings.Width;
            }
            if (_settings.Height > 0)
            {
                _window.Height = _settings.Height;
            }
            if (_settings.Left > 0 && _settings.Top > 0)
            {
                _window.Position = new PixelPoint(_settings.Left, _settings.Top);
            }

            Grammar grammar = null;

            bool   openDefaultGrammar = false;
            string packageName        = null;
            string root = null;

            if (string.IsNullOrEmpty(_settings.GrammarFileOrDirectory))
            {
                openDefaultGrammar = true;
            }
            else
            {
                try
                {
                    grammar = GrammarFactory.Open(_settings.GrammarFileOrDirectory, out packageName, out root);
                }
                catch (Exception ex)
                {
                    ShowOpenFileErrorMessage(_settings.GrammarFileOrDirectory, ex.Message);

                    _settings.OpenedGrammarFile = "";
                    openDefaultGrammar          = true;
                }
            }

            if (openDefaultGrammar)
            {
                grammar = GrammarFactory.CreateDefault();
                GrammarFactory.FillGrammarFiles(grammar, Settings.Directory, false);
                _settings.GrammarFileOrDirectory = grammar.Directory;
                _settings.Save();
            }

            _workflow = new Workflow(grammar);

            var availableRuntimes = new[]
            {
                Runtime.Java, Runtime.CSharpStandard, Runtime.CSharpOptimized, Runtime.Python2, Runtime.Python3,
                Runtime.Go, Runtime.Php
            };

            _runtimeInfoWrappers = new Dictionary <Runtime, RuntimeInfoWrapper>();

            foreach (Runtime runtime in availableRuntimes)
            {
                _runtimeInfoWrappers.Add(runtime, new RuntimeInfoWrapper(RuntimeInfo.InitOrGetRuntimeInfo(runtime)));
            }

            var list = new List <RuntimeInfoWrapper> {
                AutodetectRuntime
            };

            list.AddRange(_runtimeInfoWrappers.Values);

            Runtimes = new ObservableCollection <RuntimeInfoWrapper>(list);

            SelectedRuntime = GetAutoOrSelectedRuntime();
            PackageName     = packageName;
            Root            = root;

            InitFiles();
            if (string.IsNullOrEmpty(_settings.OpenedGrammarFile) || !grammar.Files.Contains(_settings.OpenedGrammarFile))
            {
                OpenedGrammarFile = GrammarFiles.FirstOrDefault();
            }
            else
            {
                OpenedGrammarFile = _settings.OpenedGrammarFile;
            }

            if (string.IsNullOrEmpty(_settings.OpenedTextFile))
            {
                OpenedTextFile = TextFiles.Count > 0 ? TextFiles.First() : null;
            }
            else
            {
                OpenedTextFile = new FileName(_settings.OpenedTextFile);
            }

            SetupWindowSubscriptions();
            SetupWorkflowSubscriptions();
            SetupTextBoxSubscriptions();
            SetupCommandSubscriptions();

            AutoProcessing = _settings.Autoprocessing;
        }