public void VariableDeclarationTest() { // GIVEN var source = @" function max(a: number, b: number): number do function temp: number do skip; end; return temp(); end; var a: number = 10 + 10; do var b: number = max(a + a, false); end; "; var compilerService = new CompilerService(); var analyzer = new SemanticParser(compilerService); // WHEN var node = ParseWithAbstractTreeVisitor(Compiler, source); analyzer.Visit(node as CompilationUnit); // THEN Assert.NotNull(compilerService.FindVariable("a")); Assert.AreEqual(0, compilerService.Errors.Count); Assert.AreEqual(0, compilerService.Warnings.Count); }
public void TestModify() { string raw = @"test_demon = { group = { base = 50 modifier = { value = 50 condition = { equal = {sub.a, 1} } } } }"; DataVisit.Visitor.InitVisitMap(typeof(TestData)); DataVisit.Visitor.SetVisitData(TestData.inst); Parser.Semantic.Visitor.GetValueFunc = DataVisit.Visitor.Get; Parser.Semantic.Visitor.SetValueFunc = DataVisit.Visitor.Set; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.AreEqual(100, demo.group.CalcValue()); TestData.inst.sub.a = 2; Assert.AreEqual(50, demo.group.CalcValue()); }
public void TestEqual() { string raw = @"test_demon = { condition = { equal = {1, 1} } }"; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsTrue(demo.condition.Rslt()); raw = @"test_demon = { condition = { equal = {1, 12} } }"; syntaxItem = SyntaxItem.RootParse(raw); demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsFalse(demo.condition.Rslt()); }
public void TestReduce() { string raw = @"test_demon = { selected = { reduce = {sub.a, 1} } }"; DataVisit.Visitor.InitVisitMap(typeof(TestData)); DataVisit.Visitor.SetVisitData(TestData.inst); Parser.Semantic.Visitor.GetValueFunc = DataVisit.Visitor.Get; Parser.Semantic.Visitor.SetValueFunc = DataVisit.Visitor.Set; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); TestData.inst.sub.a = 1; demo.operation.Do(); Assert.AreEqual(0, TestData.inst.sub.a); }
public ExpressionLanguageAST Parser(string expression) { LexicalParser l = new LexicalParser(); l.SetParseContent(expression); SemanticParser s = new SemanticParser(); s.SetParseContent(l.Parse()); return s.Parse(); }
public void TestAnd() { string raw = @"test_demon = { condition = { and = { less = {1, 12} greater = {1, 0} } } }"; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsTrue(demo.condition.Rslt()); raw = @"test_demon = { condition = { and = { less = {1, 12} greater = {1, 12} } } }"; syntaxItem = SyntaxItem.RootParse(raw); demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsFalse(demo.condition.Rslt()); raw = @"test_demon = { condition = { and = { less = {1, 0} greater = {1, 12} } } }"; syntaxItem = SyntaxItem.RootParse(raw); demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsFalse(demo.condition.Rslt()); }
public static T Load <T>(string fileName, string fileContent) { try { var syntaxItem = SyntaxItem.RootParse(fileContent); return(SemanticParser.DoParser <T>(syntaxItem)); } catch (Exception e) { throw new Exception($"Parse error in script:{fileName}", e); } }
private void InitializeCompiler() { _compiler = new MetaCodeCompiler(); CompilerService = CompilerService.Instance; MacroInterpreter = new MacroInterpreter(CompilerService); CodeInterpreter = new CodeInterpreter(CompilerService); CodeGenerator = new CodeGenerator(); SemanticParser = new SemanticParser(CompilerService); InitializeFunctions(); WindowTitle = "MetaCode IDE"; }
public void TestBaseValue() { string raw = @"test_demon = { group = { base = 100 } }"; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.AreEqual(100, demo.group.CalcValue()); }
public void Test1() { string raw = @"title = EVENT_TEST_TITLE desc = EVENT_TEST_DESC option = { desc = EVENT_TEST_OPTION_1_DESC }"; var syntaxItem = SyntaxItem.RootParse(raw); SemanticParser.DoParser <Modder.GEvent>(syntaxItem); }
/// <summary> /// Interprête une chaine. /// </summary> public void Eval(string str) { if (ThrowOnError) { var block = SemanticParser.ParseBlock(SyntaxicParser.Parse(str)); block.Execute(m_mainContext, null, null, null, false); } else { try { var block = SemanticParser.ParseBlock(SyntaxicParser.Parse(str)); block.Execute(m_mainContext, null, null, null, false); } catch (Exception e) { OnError(e.Message); } } }
public void Parse(string text, bool success) { var done = false; TestToken result = null; var parser = new SemanticParser(new ParserContext <TestToken, char, long>(node => { result = node; done = true; }, ((symbolId, capture, position, expectedTokens, stack) => { this.output.WriteLine($"Syntax error. Expected tokens: {string.Join(", ", expectedTokens.Select(s => s.ToString(grammar.Value.ResolveSymbol)))}"); done = true; }))); try { parser.Push(text); parser.Push(Utf16Chars.EOF); } catch (Exception ex) { this.output.WriteLine(ex.ToString()); done = true; } Assert.True(done); Assert.Equal(success, result != null); }
public void TestRiskStart() { string raw = @"test_demon = { selected = { risk.start = RISK_TEST } }"; DataVisit.Visitor.InitVisitMap(typeof(TestData)); DataVisit.Visitor.SetVisitData(TestData.inst); Parser.Semantic.Visitor.GetValueFunc = DataVisit.Visitor.Get; Parser.Semantic.Visitor.SetValueFunc = DataVisit.Visitor.Set; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); demo.operation.Do(); Assert.AreEqual("RISK_TEST", TestData.inst.risk.start); }
public TemplateAST Parse(string path) { string templateFile = Path.Combine(_engine.Path, path); _log.Debug("Begin parse template file [{0}].", templateFile); if (!File.Exists(templateFile)) ExceptionHelper.ThrowFileNotFound(templateFile); using (StreamReader r = new StreamReader(templateFile)) { string template = r.ReadToEnd(); LexicalParser lp = new LexicalParser(); lp.SetParseContent(template); SemanticParser sp = new SemanticParser(); sp.SetParseContent(lp.Parse()); TemplateAST ast = sp.Parse(); _log.Debug("Parse template file [{0}] success.", templateFile); ParseIncludeTemplate(ast); return ast; } }
public string RenderRaw(string textTemplate) { textTemplate.ThrowIfNullArgument(nameof(textTemplate)); try { LexicalParser lp = new LexicalParser(); lp.SetParseContent(textTemplate); SemanticParser sp = new SemanticParser(); sp.SetParseContent(lp.Parse()); TemplateAST ast = sp.Parse(); return this.Render(ast); } catch (ELParseException) { throw; } catch (EvalException) { throw; } catch (TemplateParseException) { throw; } catch (TemplateRenderException) { throw; } catch (Exception ex) { ExceptionHelper.ThrowWrapped(ex); return string.Empty; } }