/// <summary> /// Processes a string. /// </summary> public IEnumerable <Value> Evaluate(string input) { AbstractSyntaxNode tree = null; ArgumentException argumentEx = null; try { tree = _parser.Parse(input); Debug.Assert(tree != null, "null parse tree with no exception."); } catch (ArgumentException e) { argumentEx = e; } if (argumentEx != null) { yield return(new ErrorValue("Unable to parse: " + argumentEx.Message)); } else { foreach (Value v in Evaluate(tree)) { yield return(v); } } yield break; }
/// <summary> /// Removes intermediate AST nodes from the tree /// </summary> /// <param name="root">Root.</param> void PostProcessTree(AbstractSyntaxNode root) { // Assumes top level rule is not intermediate // Are any of my children intermediate if (root.Children != null) { foreach (var c in root.Children) { PostProcessTree(c); } var intermediates = root.Children.Where(c => c is AbstractSyntaxRuleNode && ((AbstractSyntaxRuleNode)c).ProductionRule.IsIntermediate); // foreach (var c in intermediates.ToList()) { var index = root.Children.IndexOf(c); // Remove it root.Children.Remove(c); // And adopt if (c.Children != null) { root.Children.InsertRange(index, c.Children); } } } }
public string Render(AbstractSyntaxNode ast) { this.result.Clear(); VisitAstNode(ast); return(this.result.ToString()); }
public ParsedEnumerable(AbstractSyntaxNode node, ProgramBuilder context) { _contents = new List <T>(); foreach (AbstractSyntaxNode c in node.Children) { _contents.Add((T)context.BuildNode(c)); } }
protected void WalkAstNode(AbstractSyntaxNode ast) { if (ast == null) { return; } ast.Types?.ForEach(t => VisitTypeNode(t)); ast.Namespaces?.ForEach(n => VisitNamespaceNode(n)); }
/// <summary> /// Consumes an ASTNode + Child elements to produce a JListValue node + Child elements. /// </summary> /// <returns>The ASTN ode.</returns> /// <param name="astNode">Ast node.</param> internal static Value FromASTNode(AbstractSyntaxNode astNode) { AbstractSyntaxTerminalNode terminalNode = astNode.As <AbstractSyntaxTerminalNode>(); AbstractSyntaxRuleNode ruleNode = astNode.As <AbstractSyntaxRuleNode>(); if (terminalNode != null) { if (terminalNode.TerminalSymbol.Name.Equals("NUMBER", StringComparison.Ordinal)) { long l; if (long.TryParse(terminalNode.Value, out l)) { return(new LongValue(l)); } else { return(new ErrorValue($"Invalid number '{terminalNode.Value}'.")); } } else if (terminalNode.TerminalSymbol.Name.Equals("SYMBOL", StringComparison.Ordinal)) { return(new SymbolValue(terminalNode.Value)); } else if (terminalNode.TerminalSymbol.Name.Equals("STRING", StringComparison.Ordinal)) { return(StringValue.ReadRegexString(terminalNode.Value)); } else if (terminalNode.TerminalSymbol.Name.Equals("COMMENT", StringComparison.Ordinal)) { return(new SExprValue()); } } else if (ruleNode != null) { if (new string[] { "JLisp", "SExpr" }.Contains(ruleNode.ProductionRule.Name)) { var s = new SExprValue(); BuildJLispExprValueChildren(ruleNode, s.Cell); return(s); } else if (ruleNode.ProductionRule.Name.Equals("QExpr", StringComparison.Ordinal)) { var q = new QExprValue(); BuildJLispExprValueChildren(ruleNode, q.Cell); return(q); } else if (ruleNode.ProductionRule.Name.Equals("Expr", StringComparison.Ordinal)) { return(FromASTNode(ruleNode.Children[0])); // Should only be one !! } } return(new ErrorValue("No handling for astNode: " + astNode)); }
protected virtual void PrepareUsings(AbstractSyntaxNode ast) { if (ast == null) { return; } ast.UsingCache.Clear(); ast.Usings?.ForEach(u => PrepareUsings(ast.UsingCache, u)); ast.Types?.ForEach(t => PrepareUsings(ast.UsingCache, t)); }
private void AddFileCommit(AbstractSyntaxNode member, FileCommit fileCommit) { string documentationId; if (documentationIds.TryGetValue(member.ToString(), out documentationId)) { FileCommits fileCommits; if (!fileCommitsByMember.TryGetValue(documentationId, out fileCommits)) { fileCommits = new FileCommits(); fileCommitsByMember.Add(documentationId, fileCommits); } fileCommits.Add(fileCommit); } }
/// <summary> /// Process an ast which could lead to zero or many Value Trees.. /// </summary> public IEnumerable <Value> Evaluate(AbstractSyntaxNode asTree) { if (IsMultiExpression(asTree)) { foreach (var subTree in asTree.As <AbstractSyntaxRuleNode>().Children) { var valueTree = Value.FromASTNode(subTree); yield return(Evaluate(valueTree)); } } else { var valueTree = Value.FromASTNode(asTree); yield return(Evaluate(valueTree)); } yield break; }
protected override void VisitAstNode(AbstractSyntaxNode ast) { if (ast == null) { return; } PrepareUsings(ast); VisitUsings(ast.UsingCache); if (ast.UsingCache.Usings.Count > 0 || !this.settings.BlankLine.Using) { AppendLine(); } ast.Types?.ForEach(t => VisitTypeNode(t)); ast.Namespaces?.ForEach(n => VisitNamespaceNode(n)); }
/// <summary> /// Is the tree a single expression, or multiple expressions to be evaluated in turn. /// </summary> static bool IsMultiExpression(AbstractSyntaxNode node) { // if this is a Root Node "JLisp" // And each child node is an Expr by virtue of being an SExpr then we consider this to be multiple expression // This is typically of use when dealing with files. // Each expression should be fully formed i.e "(+ 1 2 3)" not "+ 1 2 3" if (node is AbstractSyntaxRuleNode && node.As <AbstractSyntaxRuleNode>().ProductionRule.Name.Equals("JLisp") && node.As <AbstractSyntaxRuleNode>().Children.TrueForAll( c => c is AbstractSyntaxRuleNode && c.As <AbstractSyntaxRuleNode>().ProductionRule.Name.Equals("Expr") && (c.As <AbstractSyntaxRuleNode>().RulePartIndex == 4 || // S-Expr c.As <AbstractSyntaxRuleNode>().RulePartIndex == 3) // Comment ) ) { return(true); } return(false); }
public override void DefaultVisit(AbstractSyntaxNode node) { buildAction(node, fileCommit); base.DefaultVisit(node); }
public PropertyList(AbstractSyntaxNode node, ProgramBuilder context) : base(node, context) { }
public FlagCollection(AbstractSyntaxNode node, ProgramBuilder context) : base(node, context) { }
public ExpressionList(AbstractSyntaxNode node, ProgramBuilder context) : base(node, context) { }
public override void DefaultVisit(AbstractSyntaxNode node) { buildAction(node, fileCommit); base.DefaultVisit(node); }
private void AddFileCommit(AbstractSyntaxNode member, FileCommit fileCommit) { string documentationId; if (documentationIds.TryGetValue(member.ToString(), out documentationId)) { FileCommits fileCommits; if (!fileCommitsByMember.TryGetValue(documentationId, out fileCommits)) { fileCommits = new FileCommits(); fileCommitsByMember.Add(documentationId, fileCommits); } fileCommits.Add(fileCommit); } }
protected virtual void VisitAstNode(AbstractSyntaxNode ast) { PrepareUsings(ast); VisitUsings(ast.UsingCache); WalkAstNode(ast); }