public CodeStream Compile(ParseNode node) { var child = Push(node); Code += Compiler(child); return Code; }
private void Expacc() { _currentNode.AddChild(new ParseNode(ParseEnum.ExpressionAccent)); _currentNode = _currentNode.getChildren()[_currentNode.getChildren().Count - 1]; if (!_lex.EndOfInput) { if (_current is AddSub) { _currentNode.AddChild(new ParseNode(ParseEnum.Operator, _current.GetValue())); _current = _lex.GetNextToken(); Term(); Expacc(); } else if (_current is Equals) { _currentNode.AddChild(new ParseNode(ParseEnum.Equals)); _current = _lex.GetNextToken(); Expressie(); } else { _currentNode.AddChild(new ParseNode(ParseEnum.Empty)); } } _currentNode = _currentNode.GetParent(); }
public void OutputDeclaration(ParseNode node, string indent) { if (node.RuleName == "declaration") { Assert(node.Count == 1); node = node[0]; Assert(node.Count >= 2); Assert(node[0].RuleName == "comment_set"); OutputPrefixComment(node[0], indent); switch (node[1].RuleName) { case "pp_directive": WriteLine("pp_directive: " + node[1]); break; case "declaration_content": OutputSuffixComment(node[3], indent); OutputDeclarationContent(node[1]); break; case "semicolon": WriteLine("empty declaration"); break; default: throw new Exception("Unrecognized kind of declaration"); } } foreach (ParseNode child in node) OutputDeclaration(child, indent + " "); }
private void ParseCompanyMatchedClause(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.CompanyMatchedClause), "CompanyMatchedClause"); parent.Nodes.Add(node); tok = scanner.Scan(TokenType.CompanyMatchedKeyword); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.CompanyMatchedKeyword) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.CompanyMatchedKeyword.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } tok = scanner.Scan(TokenType.NUMBER); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.NUMBER) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.NUMBER.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } parent.Token.UpdateRange(node.Token); }
private void Parseadd_stmt(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.add_stmt), "add_stmt"); parent.Nodes.Add(node); tok = scanner.Scan(TokenType.ADD); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.ADD) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.ADD.ToString(), 0x1001, tok)); return; } Parseexpr(node); tok = scanner.Scan(TokenType.EOI); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.EOI) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.EOI.ToString(), 0x1001, tok)); return; } parent.Token.UpdateRange(node.Token); }
private void ParseStart(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.Start), "Start"); parent.Nodes.Add(node); tok = scanner.LookAhead(TokenType.COMPLEMENT, TokenType.STATE, TokenType.BROPEN); if (tok.Type == TokenType.COMPLEMENT || tok.Type == TokenType.STATE || tok.Type == TokenType.BROPEN) { ParseUnionExpr(node); } tok = scanner.Scan(TokenType.EOF); if (tok.Type != TokenType.EOF) tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.EOF.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); parent.Token.UpdateRange(node.Token); }
public void VisitDirective(ParseNode node) { NodeStartHousekeeping(node); // For now, let the compiler decide if the compiler directive is in the wrong place, // not the parser. Therefore the parser treats it like a normal statement and here in // the compiler we'll decide per-directive which directives can go where: ParseNode directiveNode = node.Nodes[0]; // a directive contains the exact directive node nested one step inside it. if (directiveNode.Nodes.Count < 2) throw new KOSCompileException(new LineCol(lastLine, lastColumn), "Kerboscript compiler directive ('@') without a keyword after it."); switch (directiveNode.Nodes[1].Token.Type) { case TokenType.LAZYGLOBAL: VisitLazyGlobalDirective(directiveNode); break; // There is room for expansion here if we want to add more compiler directives. default: throw new KOSCompileException(new LineCol(lastLine, lastColumn), "Kerboscript compiler directive @"+directiveNode.Nodes[1].Text+" is unknown."); } }
} // NonTerminalSymbol: Start private void ParseAddExpr(ParseNode parent) // NonTerminalSymbol: AddExpr { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.AddExpr), "AddExpr"); parent.Nodes.Add(node); // Concat Rule ParseMultExpr(node); // NonTerminal Rule: MultExpr // Concat Rule tok = scanner.LookAhead(TokenType.PLUSMINUS); // ZeroOrMore Rule while (tok.Type == TokenType.PLUSMINUS) { // Concat Rule tok = scanner.Scan(TokenType.PLUSMINUS); // Terminal Rule: PLUSMINUS if (tok.Type != TokenType.PLUSMINUS) tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.PLUSMINUS.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); // Concat Rule ParseMultExpr(node); // NonTerminal Rule: MultExpr tok = scanner.LookAhead(); // ZeroOrMore Rule } parent.Token.UpdateRange(node.Token); } // NonTerminalSymbol: AddExpr
private void ParseStart(ParseNode parent) // NonTerminalSymbol: Start { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.Start), "Start"); parent.Nodes.Add(node); // Concat Rule do { // OneOrMore Rule ParseAddExpr(node); // NonTerminal Rule: AddExpr tok = scanner.LookAhead(TokenType.NUMBER, TokenType.BROPEN); // OneOrMore Rule || tok.Type == TokenType.NUMBER || tok.Type == TokenType.BROPEN); // OneOrMore Rule // Concat Rule tok = scanner.Scan(TokenType.EOF); // Terminal Rule: EOF if (tok.Type != TokenType.EOF) tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.EOF.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); parent.Token.UpdateRange(node.Token); } // NonTerminalSymbol: Start
private WhenSenderAndProjectCompanyMatched(ParseNode clauseNode, IStorageRepository storage, UserRepository userRepository) { _projectId = Int32.Parse(ClauseFactory.FindRecursive(TokenType.NUMBER, clauseNode).Token.Text); _storage = storage; _userRepository = userRepository; }
public UserFunction(ParseNode originalNode) { codePart = new CodePart(); functions = new Dictionary<int, UserFunctionCodeFragment>(); newFunctions = new List<UserFunctionCodeFragment>(); OriginalNode = originalNode; }
public void OutputDeclarationContent(ParseNode node) { Assert(node.RuleName == "declaration_content"); string s = node.Aggregate("content: ", OutputChild); WriteLine(s); }
private void Parseand_expr(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.and_expr), "and_expr"); parent.Nodes.Add(node); Parsecompare_expr(node); tok = scanner.LookAhead(TokenType.AND); while (tok.Type == TokenType.AND) { tok = scanner.Scan(TokenType.AND); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.AND) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.AND.ToString(), 0x1001, tok)); return; } Parsecompare_expr(node); tok = scanner.LookAhead(TokenType.AND); } parent.Token.UpdateRange(node.Token); }
private void ParseStart(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.Start), "Start"); parent.Nodes.Add(node); tok = scanner.LookAhead(TokenType.VARIABLE, TokenType.STRING, TokenType.NUMBER, TokenType.BOOLEAN, TokenType.BROPEN); if (tok.Type == TokenType.VARIABLE || tok.Type == TokenType.STRING || tok.Type == TokenType.NUMBER || tok.Type == TokenType.BOOLEAN || tok.Type == TokenType.BROPEN) { ParseExpr(node); } tok = scanner.Scan(TokenType.EOF); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.EOF) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.EOF.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } parent.Token.UpdateRange(node.Token); }
public DynamicFunction(string name, ParseNode node, Variables args, int minParameters = 0, int maxParameters = 0) { Node = node; Arguments = args; MinParameters = minParameters; MaxParameters = maxParameters; }
/// <summary> /// Constructs a parser state, that manages a pointer to the text. /// </summary> /// <param name="text"></param> public ParserState(string text) { this.text = text; ParseNode root = new ParseNode(null, null, text, 0); root.Complete(text.Length); nodes.Push(root); }
private void ParseSepExpr(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.SepExpr), "SepExpr"); parent.Nodes.Add(node); ParseOrExpr(node); tok = scanner.LookAhead(TokenType.SEPARATOR); while (tok.Type == TokenType.SEPARATOR) { tok = scanner.Scan(TokenType.SEPARATOR); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.SEPARATOR) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.SEPARATOR.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } ParseOrExpr(node); tok = scanner.LookAhead(TokenType.SEPARATOR); } parent.Token.UpdateRange(node.Token); }
private void ParseNodeSpec(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.NodeSpec), "NodeSpec"); parent.Nodes.Add(node); tok = scanner.Scan(TokenType.IDENTIFIER); n = node.CreateNode(tok, tok.ToString()); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.IDENTIFIER) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.IDENTIFIER.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } tok = scanner.Scan(TokenType.IDENTIFIER); n = node.CreateNode(tok, tok.ToString()); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.IDENTIFIER) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.IDENTIFIER.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } tok = scanner.Scan(TokenType.IDENTIFIER); n = node.CreateNode(tok, tok.ToString()); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.IDENTIFIER) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.IDENTIFIER.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } ParseSuccessors(node); tok = scanner.LookAhead(TokenType.STRING); if (tok.Type == TokenType.STRING) { tok = scanner.Scan(TokenType.STRING); n = node.CreateNode(tok, tok.ToString()); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.STRING) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.STRING.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } } tok = scanner.Scan(TokenType.SEMICOLON); n = node.CreateNode(tok, tok.ToString()); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.SEMICOLON) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.SEMICOLON.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } parent.Token.UpdateRange(node.Token); }
/// <summary> /// Constructs a parser state, that manages a pointer to the text. /// </summary> /// <param name="text"></param> public ParserState(string text) { CreateNodes = true; _text = text; var root = new ParseNode(null, null, text, 0); root.Complete(text.Length); _nodes.Push(root); }
protected ThenClause(ParseNode clauseNode, ITpBus bus, IStorageRepository storage) { _bus = bus; _storage = storage; var projectIdNode = ClauseFactory.FindRecursive(TokenType.NUMBER, clauseNode); _projectId = Int32.Parse(projectIdNode.Token.Text); }
private void ParseArithmeticOperator(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.ArithmeticOperator), "ArithmeticOperator"); parent.Nodes.Add(node); tok = scanner.LookAhead(TokenType.PLUS, TokenType.MINUS, TokenType.ASTERISK, TokenType.FSLASH, TokenType.PERCENT); switch (tok.Type) { case TokenType.PLUS: tok = scanner.Scan(TokenType.PLUS); if (tok.Type != TokenType.PLUS) tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.PLUS.ToString(), 0x1001, tok.LinePos, tok.ColumnPos, tok.StartPos, tok.Length)); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); break; case TokenType.MINUS: tok = scanner.Scan(TokenType.MINUS); if (tok.Type != TokenType.MINUS) tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.MINUS.ToString(), 0x1001, tok.LinePos, tok.ColumnPos, tok.StartPos, tok.Length)); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); break; case TokenType.ASTERISK: tok = scanner.Scan(TokenType.ASTERISK); if (tok.Type != TokenType.ASTERISK) tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.ASTERISK.ToString(), 0x1001, tok.LinePos, tok.ColumnPos, tok.StartPos, tok.Length)); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); break; case TokenType.FSLASH: tok = scanner.Scan(TokenType.FSLASH); if (tok.Type != TokenType.FSLASH) tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.FSLASH.ToString(), 0x1001, tok.LinePos, tok.ColumnPos, tok.StartPos, tok.Length)); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); break; case TokenType.PERCENT: tok = scanner.Scan(TokenType.PERCENT); if (tok.Type != TokenType.PERCENT) tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.PERCENT.ToString(), 0x1001, tok.LinePos, tok.ColumnPos, tok.StartPos, tok.Length)); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); break; default: tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found.", 0x0002, tok.LinePos, tok.ColumnPos, tok.StartPos, tok.Length)); break; } parent.Token.UpdateRange(node.Token); }
public Assembler(string source) : this() { var parser = new ParserState(source); bool match = _grammar.program.Match(parser); if (match == false) throw new Exception("Cannot parse source file"); _root = parser.GetRoot(); }
private static void PopulateNode(TreeNode node, ParseNode start) { foreach (ParseNode ipn in start.Nodes) { TreeNode tn = new TreeNode(ipn.Text); tn.Tag = ipn; node.Nodes.Add(tn); PopulateNode(tn, ipn); } }
private void ParseExpr(ParseNode parent) { //Token tok; //ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.Expr), "Expr"); parent.Nodes.Add(node); ParseSepExpr(node); parent.Token.UpdateRange(node.Token); }
// NonTerminalSymbol: AdditiveExpression private void ParseAdditiveExpression(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.AdditiveExpression), "AdditiveExpression"); parent.Nodes.Add(node); // Concat Rule ParseMultiplicativeExpression(node); // NonTerminal Rule: MultiplicativeExpression // Concat Rule tok = scanner.LookAhead(TokenType.PLUS, TokenType.MINUS); // ZeroOrMore Rule while (tok.Type == TokenType.PLUS || tok.Type == TokenType.MINUS) { // Concat Rule tok = scanner.LookAhead(TokenType.PLUS, TokenType.MINUS); // Choice Rule switch (tok.Type) { // Choice Rule case TokenType.PLUS: tok = scanner.Scan(TokenType.PLUS); // Terminal Rule: PLUS n = node.CreateNode(tok, tok.ToString()); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.PLUS) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.PLUS.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } break; case TokenType.MINUS: tok = scanner.Scan(TokenType.MINUS); // Terminal Rule: MINUS n = node.CreateNode(tok, tok.ToString()); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.MINUS) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.MINUS.ToString(), 0x1001, 0, tok.StartPos, tok.StartPos, tok.Length)); return; } break; default: tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found.", 0x0002, 0, tok.StartPos, tok.StartPos, tok.Length)); break; } // Choice Rule // Concat Rule ParseMultiplicativeExpression(node); // NonTerminal Rule: MultiplicativeExpression tok = scanner.LookAhead(TokenType.PLUS, TokenType.MINUS); // ZeroOrMore Rule } parent.Token.UpdateRange(node.Token); }
public void OutputPrefixComment(ParseNode node, string indent) { Assert(node.RuleName == "comment_set"); string r = ""; foreach (ParseNode x in node.GetHierarchy()) if (x.RuleName == "comment") r += x.ToString(); if (r.Length > 0) WriteLine("prefix comment: " + r); }
private void ParseStart(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.Start), "Start"); parent.Nodes.Add(node); tok = scanner.LookAhead(TokenType.Code, TokenType.Technique, TokenType.Sampler); while (tok.Type == TokenType.Code || tok.Type == TokenType.Technique || tok.Type == TokenType.Sampler) { tok = scanner.LookAhead(TokenType.Code, TokenType.Technique, TokenType.Sampler); switch (tok.Type) { case TokenType.Code: tok = scanner.Scan(TokenType.Code); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.Code) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.Code.ToString(), 0x1001, tok)); return; } break; case TokenType.Technique: ParseTechnique_Declaration(node); break; case TokenType.Sampler: ParseSampler_Declaration(node); break; default: tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found.", 0x0002, tok)); break; } tok = scanner.LookAhead(TokenType.Code, TokenType.Technique, TokenType.Sampler); } tok = scanner.Scan(TokenType.EndOfFile); n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.EndOfFile) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.EndOfFile.ToString(), 0x1001, tok)); return; } parent.Token.UpdateRange(node.Token); }
private void ParseStart(ParseNode parent) // NonTerminalSymbol: Start { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.Start), "Start"); parent.Nodes.Add(node); // Concat Rule tok = scanner.LookAhead(TokenType.Code, TokenType.Technique, TokenType.Sampler); // ZeroOrMore Rule while (tok.Type == TokenType.Code || tok.Type == TokenType.Technique || tok.Type == TokenType.Sampler) { tok = scanner.LookAhead(TokenType.Code, TokenType.Technique, TokenType.Sampler); // Choice Rule switch (tok.Type) { // Choice Rule case TokenType.Code: tok = scanner.Scan(TokenType.Code); // Terminal Rule: Code n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.Code) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.Code.ToString(), 0x1001, tok)); return; } break; case TokenType.Technique: ParseTechnique_Declaration(node); // NonTerminal Rule: Technique_Declaration break; case TokenType.Sampler: ParseSampler_Declaration(node); // NonTerminal Rule: Sampler_Declaration break; default: tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected Code, Technique, or Sampler.", 0x0002, tok)); break; } // Choice Rule tok = scanner.LookAhead(TokenType.Code, TokenType.Technique, TokenType.Sampler); // ZeroOrMore Rule } // Concat Rule tok = scanner.Scan(TokenType.EndOfFile); // Terminal Rule: EndOfFile n = node.CreateNode(tok, tok.ToString() ); node.Token.UpdateRange(tok); node.Nodes.Add(n); if (tok.Type != TokenType.EndOfFile) { tree.Errors.Add(new ParseError("Unexpected token '" + tok.Text.Replace("\n", "") + "' found. Expected " + TokenType.EndOfFile.ToString(), 0x1001, tok)); return; } parent.Token.UpdateRange(node.Token); } // NonTerminalSymbol: Start
public CompilerContext Push(ParseNode node) { if ( node == null ) throw new ArgumentNullException(nameof(node)); return new CompilerContext { Code = new CodeStream(), Node = node, Parent = this, JumpExpression = OpCode.Noop, }; }
private void ParseExpression(ParseNode parent) { Token tok; ParseNode n; ParseNode node = parent.CreateNode(scanner.GetToken(TokenType.Expression), "Expression"); parent.Nodes.Add(node); ParseConditionalExpression(node); parent.Token.UpdateRange(node.Token); }
private DeclarationStatement DeclarationStatement(ParseNode node) { return(node.Children .GetAndMove(VarDeclarations) .Pipe(declarations => new DeclarationStatement(declarations))); }
private ExpressionStatement ExpressionStatement(ParseNode node) { return(node.Children .GetAndMove(StatementExpression) .Pipe(expr => new ExpressionStatement(expr))); }
public static void ReportExpressionError(this IErrorHandler errorHandler, string message, ParseNode node) { CompilerError error = new CompilerError((ushort)CompilerErrorCode.ExpressionError, message, node.Token.SourcePath, node.Token.Position.Line, node.Token.Position.Column); errorHandler.ReportError(error); }
public TypeSymbol ResolveType(ParseNode node, ISymbolTable symbolTable, Symbol contextSymbol) { if (node is IntrinsicTypeNode) { IntrinsicType intrinsicType = IntrinsicType.Integer; IntrinsicTypeNode intrinsicTypeNode = (IntrinsicTypeNode)node; switch (intrinsicTypeNode.Type) { case TokenType.Object: intrinsicType = IntrinsicType.Object; break; case TokenType.Bool: intrinsicType = IntrinsicType.Boolean; break; case TokenType.String: case TokenType.Char: intrinsicType = IntrinsicType.String; break; case TokenType.Int: intrinsicType = IntrinsicType.Integer; break; case TokenType.UInt: intrinsicType = IntrinsicType.UnsignedInteger; break; case TokenType.Long: intrinsicType = IntrinsicType.Long; break; case TokenType.ULong: intrinsicType = IntrinsicType.UnsignedLong; break; case TokenType.Short: intrinsicType = IntrinsicType.Short; break; case TokenType.UShort: intrinsicType = IntrinsicType.UnsignedShort; break; case TokenType.Byte: intrinsicType = IntrinsicType.Byte; break; case TokenType.SByte: intrinsicType = IntrinsicType.SignedByte; break; case TokenType.Float: intrinsicType = IntrinsicType.Single; break; case TokenType.Decimal: intrinsicType = IntrinsicType.Decimal; break; case TokenType.Double: intrinsicType = IntrinsicType.Double; break; case TokenType.Delegate: intrinsicType = IntrinsicType.Delegate; break; case TokenType.Void: intrinsicType = IntrinsicType.Void; break; } TypeSymbol typeSymbol = ResolveIntrinsicType(intrinsicType); if (intrinsicTypeNode.IsNullable) { TypeSymbol nullableType = ResolveIntrinsicType(IntrinsicType.Nullable); typeSymbol = CreateGenericTypeSymbol(nullableType, new List <TypeSymbol>() { typeSymbol }); } return(typeSymbol); } else if (node is ArrayTypeNode) { ArrayTypeNode arrayTypeNode = (ArrayTypeNode)node; TypeSymbol itemTypeSymbol = ResolveType(arrayTypeNode.BaseType, symbolTable, contextSymbol); Debug.Assert(itemTypeSymbol != null); return(CreateArrayTypeSymbol(itemTypeSymbol)); } else if (node is GenericNameNode) { GenericNameNode genericNameNode = (GenericNameNode)node; string genericTypeName = genericNameNode.Name + "`" + genericNameNode.TypeArguments.Count; TypeSymbol templateType = (TypeSymbol)symbolTable.FindSymbol(genericTypeName, contextSymbol, SymbolFilter.Types); List <TypeSymbol> typeArguments = new List <TypeSymbol>(); foreach (ParseNode argNode in genericNameNode.TypeArguments) { TypeSymbol argType = ResolveType(argNode, symbolTable, contextSymbol); typeArguments.Add(argType); } TypeSymbol resolvedSymbol = CreateGenericTypeSymbol(templateType, typeArguments); Debug.Assert(resolvedSymbol != null); return(resolvedSymbol); } else { Debug.Assert(node is NameNode); NameNode nameNode = (NameNode)node; return((TypeSymbol)symbolTable.FindSymbol(nameNode.Name, contextSymbol, SymbolFilter.Types)); } }
public static IOpenApiAny LoadAny(ParseNode node) { return(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny())); }
/// <returns>returns true to indicate the children should be skipped</returns> private bool Evaluation(ParseNode node) { switch (node.Token.Type) { case TokenType.Namespace: //the actual change of scope is done with NamespaceBegin Helpers.GetAndValidateTagsNames(node.Nodes.First(), node.Nodes.Last()); break; case TokenType.NamespaceBegin: //switch to the sub config currentObjectStack.Clear(); currentObjectStack.Push(GetCorrespondingSubConf(Helpers.GetTextForNode(node, TokenType.NAME))); return(true); case TokenType.NamespaceEnd: //return to main config currentObjectStack.Clear(); currentObjectStack.Push(mainConf); return(true); /**************************/ case TokenType.SimpleDeclaration: currentProperty = Helpers.FindProperty(currentObject, Helpers.GetTextForNode(node, TokenType.NAME)); break; case TokenType.QUOTEDCONTENT: Helpers.ConvertAndSetProperty(currentObject, currentProperty, Helpers.ParseQuotedString(node.Token.Text.Trim()), Converters); return(true); case TokenType.SINGLELINECONTENT: Helpers.ConvertAndSetProperty(currentObject, currentProperty, node.Token.Text.Trim(), Converters); return(true); /**************************/ case TokenType.MultiLineDeclaration: currentProperty = Helpers.FindProperty(currentObject, node); string parsedMultiLineString = Helpers.ParseMultiLinesRawString(Helpers.GetTextForNode(node, TokenType.MULTILINECONTENT)); Helpers.ConvertAndSetProperty(currentObject, currentProperty, parsedMultiLineString, Converters); return(true); /**************************/ //Complex Declaration case TokenType.TagBegin: string nextName = Helpers.GetTextForNode(node, TokenType.NAME); PropertyInfo nextProperty; //WARNING: if the current object is an IEnumerable and have a property with the same name as the one defined for the items in the enumerable, //the property will be set instead of added to the enumerable. //NOTE: actually this is not possible, since all the enumerable initalized by the configurator are Lists... bool propertyNameFound = Helpers.TryFindProperty(currentObject, nextName, out nextProperty); if (propertyNameFound) { currentProperty = nextProperty; currentObjectStack.Push(Helpers.GetOrCreateObject(currentObject, currentProperty)); } else { //the name does not match any property... if (Helpers.IsEnumerable(currentObject.GetType())) { // add a new complex item to the current object, and set it as the new current var newObject = Helpers.CreateObject(currentObject.GetType().GenericTypeArguments.Single()); IList collection = (IList)currentObject; collection.Add(newObject); currentObjectStack.Push(newObject); } else { throw new EvaluationException("No matching property found for a complex declaration!", node.Token); } } return(true); case TokenType.TagEnd: currentObjectStack.Pop(); return(true); /**************************/ case TokenType.ListDeclaration: currentProperty = Helpers.FindProperty(currentObject, node); break; case TokenType.MultiLineItem: string parsedMultiLineItem = Helpers.ParseMultiLinesRawString(Helpers.GetTextForNode(node, TokenType.MULTILINECONTENT)); Helpers.ConvertAndAddToProperty(currentObject, currentProperty, parsedMultiLineItem, Converters); return(true); case TokenType.SIMPLEITEM: Helpers.ConvertAndAddToProperty(currentObject, currentProperty, node.Token.Text.Trim(), Converters); return(true); case TokenType.QUOTEDITEM: Helpers.ConvertAndAddToProperty(currentObject, currentProperty, Helpers.ParseQuotedString(node.Token.Text.Trim()), Converters); return(true); /**************************/ default: //ignored break; } return(false); //the children will be evaluated }
private ParseTree(ParseNode root, Dictionary <string, List <ParseNode> > symbolTable) { this._root = root; this._symbolTable = symbolTable; }
private PrimitiveTypeNode PrimitiveType(ParseNode node) { return(node.Children .GetAndMove(ParseNodeTag.Terminal) .Pipe(terminal => new PrimitiveTypeNode(terminal.Token))); }
protected override object EvalSymbol(ParseTree tree, params object[] paramlist) { ParseNode last = Nodes[Nodes.Count - 1]; if (last.Token.Type == TokenType.UNARYOPER) { Rule unaryRule; string oper = last.Token.Text.Trim(); if (oper == "*") { unaryRule = new Rule(RuleType.ZeroOrMore); } else if (oper == "+") { unaryRule = new Rule(RuleType.OneOrMore); } else if (oper == "?") { unaryRule = new Rule(RuleType.Option); } else { throw new NotImplementedException("unknown unary operator"); } if (Nodes[0].Token.Type == TokenType.BRACKETOPEN) { Rule rule = (Rule)Nodes[1].Eval(tree, paramlist); unaryRule.Rules.Add(rule); } else { Grammar g = (Grammar)paramlist[0]; if (Nodes[0].Token.Type == TokenType.IDENTIFIER) { Symbol s = g.Symbols.Find(Nodes[0].Token.Text); if (s == null) { tree.Errors.Add(new ParseError("Symbol '" + Nodes[0].Token.Text + "' is not declared. ", 0x1042, Nodes[0])); } Rule r = new Rule(s); unaryRule.Rules.Add(r); } } return(unaryRule); } if (Nodes[0].Token.Type == TokenType.BRACKETOPEN) { // create subrule syntax tree return(Nodes[1].Eval(tree, paramlist)); } else { Grammar g = (Grammar)paramlist[0]; Symbol s = (Symbol)g.Symbols.Find(Nodes[0].Token.Text); if (s == null) { tree.Errors.Add(new ParseError("Symbol '" + Nodes[0].Token.Text + "' is not declared.", 0x1043, Nodes[0])); } return(new Rule(s)); } }
public ExpressionBuildException(ParseNode node, string message) : base(message) { Node = node; }
private void ModifyRoot(ArrayList path, ref ParseNode node) { bool still = false; switch (node.Goal) { case "CS": { if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; } } break; case "NNC": case "NN": { ParseNode fch = (ParseNode)node.Children[0]; if (fch.Goal == "N") { node = (ParseNode)node.Children[0]; } else { node.Goal = "N"; } } break; case "NP": case "NPC": case "NPF": case "NPADJ": { node.Goal = "NP"; ParseNode fch = (ParseNode)node.Children[0]; if (fch.Goal == "NP" || fch.Goal == "NPC") { node.Children.RemoveAt(0); node.Children.InsertRange(0, fch.Children); } } break; case "ABPH": node.Goal = "ABS_PH"; break; case "PRPH": node.Goal = "PAR_PH"; break; case "ADVC": node.Goal = "ADV_CL"; break; case "NC": node.Goal = "N_CL"; break; case "ADJC": node.Goal = "ADJ_CL"; break; case "PRP": node.Goal = "PREP_PH"; break; case "INFPO": case "INFPH": node.Goal = "INF_PH"; break; case "PPN": node.Goal = "PRO_N"; break; case "CMPAJ": case "BADJ": { ParseNode fch = (ParseNode)node.Children[0]; if (fch.Goal.EndsWith("ADJ")) { node = (ParseNode)node.Children[0]; } node.Goal = "ADJ"; } break; case "FADJ": node.Goal = "ADJ"; break; case "FAVJ": case "AVJ": { ParseNode fch = (ParseNode)node.Children[0]; if (fch.Goal.EndsWith("ADJS")) { node = (ParseNode)node.Children[0]; still = true; } else { node.Goal = "ADV_ADJ"; } } break; case "CMPAV": { node = (ParseNode)node.Children[0]; node.Goal = "ADV"; } break; case "CMPAVJ": { node = (ParseNode)node.Children[0]; still = true; } break; case "XV": node.Goal = "AUX_V"; break; case "IVP": case "SVP": case "PVP": case "GVP": node.Goal = "VP"; break; case "IPRD": case "SPRD": case "PPRD": case "GPRD": node.Goal = "PRD"; break; case "ADV": { ParseNode fch = (ParseNode)node.Children[0]; if (fch.Goal.EndsWith("ADV")) { node = (ParseNode)node.Children[0]; node.Goal = "ADV"; } } break; case "PADJ": case "SADJ": case "CADJ": node.Goal = "ADJ"; break; case "PADV": node.Goal = "ADV"; break; case "ARC": node.Goal = "DET"; break; case "SRPN": node.Goal = "RPN"; break; case "PSRPN": node.Goal = "RPN"; break; case "RPN": { if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; still = true; } } break; case "IPRDS": case "GPRDS": case "SPRDS": case "PPRDS": case "PRDS": { node.Children = GetListItems(node); if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; still = true; } else { node.Goal = "PRD_LST"; } } break; case "PRPHS": { node.Children = GetListItems(node); if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; still = true; } else { node.Goal = "PAR_PH_LST"; } } break; case "CMPAJS": case "FADJS": case "BADJS": { node.Children = GetListItems(node); if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; still = true; } else { node.Goal = "ADJ_LST"; } } break; case "ADVS": case "CMPAVS": { node.Children = GetListItems(node); if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; still = true; } else { node.Goal = "ADV_LST"; } } break; case "LPRPS": case "PRPS": { node.Children = GetListItems(node); if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; still = true; } else { node.Goal = "PREP_PH_LST"; } } break; case "SBJ": case "OBJ": { node.Children = GetListItems(node); if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; still = true; } } break; case "CMPS": { node.Children = GetListItems(node); if (node.Children.Count == 1) { node = (ParseNode)node.Children[0]; still = true; } else { node.Goal = "CMP_LST"; } } break; } //end if (still) { ModifyRoot(path, ref node); } else if (node.Children != null) { path.Add(node); for (int i = 0; i < node.Children.Count; i++) { ParseNode child = (ParseNode)node.Children[i]; ModifyRoot(path, ref child); node.Children[i] = child; } path.RemoveAt(path.Count - 1); } }
private void ViewTrees(ArrayList Trees, TextBox textBox, TreeView treeView) { ArrayList ParseTrees = new ArrayList(); foreach (ParseTree ps in Trees) { ParseTrees.Add(ModifyParseTree(ps)); } // ParseTrees=Trees; treeView.Nodes.Clear(); if (ParseTrees == null) { return; } foreach (ParseTree tree in ParseTrees) { if (tree == null) { continue; } TreeNode root = new TreeNode(); root.Tag = tree.Score.ToString(); Stack nodes = new Stack(); Stack items = new Stack(); nodes.Push(tree.Root); items.Push(root); string[] words = textBox.Text.Split(' '); while (nodes.Count > 0) { ParseNode node = (ParseNode)nodes.Pop(); TreeNode item = (TreeNode)items.Pop(); if (node.Senses != null) { string vlist = ""; foreach (NodeSense s in node.Senses) { vlist += s.Sense + "\n"; } item.Tag = vlist; } item.Text = node.Goal + " : "; for (int i = node.Start; i < node.End; i++) { item.Text += " " + tree.Words[i].ToString().ToLower(); } if (node.Children == null) { continue; } for (int i = 0; i < node.Children.Count; i++) { nodes.Push(node.Children[i]); TreeNode t = new TreeNode(); item.Nodes.Add(t); items.Push(t); } } root.ExpandAll(); root.Collapse(); treeView1.Nodes.Add(root); } }
public static void ReportNodeValidationError(this IErrorHandler errorHandler, string message, ParseNode parseNode) { string location = parseNode?.Token?.Location; CompilerError error = new CompilerError((ushort)CompilerErrorCode.NodeValidationError, message, location, parseNode?.Token?.Position.Line, parseNode?.Token?.Position.Column); errorHandler.ReportError(error); }
public static void ReportUnsupportedFeatureError(this IErrorHandler errorHandler, string feature, ParseNode node) { string message = $"{feature} is not supported."; CompilerError error = new CompilerError((ushort)CompilerErrorCode.UnsupportedFeatureError, message, node.Token.SourcePath, node.Token.Position.Line, node.Token.Position.Column); errorHandler.ReportError(error); }
public Parser(String invoer) { _lex = new LexicalScanner(invoer); _currentNode = _start; }
/// <summary> /// Attempt to parse an RPN sequence of Parse tokens. If successful return the reslting /// tree, else return null and populate the Error and ErrorMessage parameters. /// </summary> /// <param name="Input">RPN sequence of Tokens. </param> /// <param name="Error">Container for Token of Error</param> /// <param name="ErrorMessage">Error message regarding violating token. </param> /// <returns>A binary expression tree if succesful, otherwise null. </returns> public static ParseTree Parse(List <ParseToken> Input, out ParseToken Error, out string ErrorMessage) { Dictionary <string, List <ParseNode> > symbolTable = new Dictionary <string, List <ParseNode> >(); Stack <ParseNode> wSet = new Stack <ParseNode>(); //Stack for processing. ParseNode c; foreach (ParseToken pt in Input) { try { c = new ParseNode(pt); if (c.IsOperator) { //NOT is the only operator that requires //a single operand. if (c.Operation == Operation.NOT) { c.Left = wSet.Pop(); } else { //Right was placed on the stack last //This order does not matter except for IF operators c.Right = wSet.Pop(); c.Left = wSet.Pop(); } //and push it on the stack (fall through) } else { //if its an operand we want to add it to our symbol table. try { symbolTable.Add(c.Token.symbol, new List <ParseNode>() { c }); } catch (ArgumentException) { symbolTable[c.Token.symbol].Add(c); } //Thrown when the key is already present, so at it to the current list. } wSet.Push(c); } catch (InvalidOperationException) { //An operator was not able to pop its needed //operands from the working set. Error = pt; ErrorMessage = string.Format("Too few operands for {0}.", pt.symbol); return(null); } } if (wSet.Count == 0) { //We lost out root? throw new Exception(); } if (wSet.Count > 1) { //If there are still things in the wSet more than //one thing left in the stack there was too many //operands for the operators. while (wSet.Peek().IsOperator&& wSet.Count > 1) { wSet.Pop(); } Error = wSet.Peek().Token; ErrorMessage = string.Format( "Unused operands starting w/ {0}.", wSet.Peek().Token.symbol); return(null); } Error = null; ErrorMessage = null; return(new ParseTree(wSet.Pop(), symbolTable)); }
private int ArrayRank(ParseNode node) { return(node.Children.Count()); }
public ParseError(string message, int code, ParseNode node) : this(message, code, 0, node.Token.StartPos, node.Token.StartPos, node.Token.Length) { }
private Literal Literal(ParseNode node) { return(node.Children .GetAndMove(ParseNodeTag.Terminal) .Pipe(terminal => new Literal(terminal.Token))); }
private static string LoadString(ParseNode node) { return(node.GetScalarValue()); }
private QualifiedIdentifier QualifiedIdentifier(ParseNode node) { return(node.Children .Select(x => x.Token.Lexeme) .Pipe(x => new QualifiedIdentifier(x))); }
private Expression ParenthesisExpression(ParseNode node) { return(node.Children .Skip(TokenTag.OPEN_PAREN) .GetAndMove(Expression)); }
private PrefixDecrement PrefixDecrement(ParseNode node, bool isStatementExpression = false) { return(node.Children .GetAndMove(Expression) .Pipe(operand => new PrefixDecrement(operand, isStatementExpression))); }
public void Visit(ParseNode node) => VisitInternal(node);
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("usage: rockstart.exe rockstarfile"); Environment.Exit(1); } if (!File.Exists(args[0])) { Console.WriteLine("Unable to open file"); Environment.Exit(1); } string text = File.ReadAllText(args[0]); Tokeniser tok = new Tokeniser(); List <Token> tokens = tok.Tokenise(text); Parser parser = new Parser(); ParseNode root = parser.Parse(tokens); if (root != null) { /*BuildEnvironment env = new BuildEnvironment(); * * CSResult result = root.BuildToCS(env); * string code = ""; * result.GeneratedCS.ForEach(cs => code += cs.CS + "\n"); * Console.WriteLine(code);*/ try { InterpreterEnvironment env = new InterpreterEnvironment(); root.Interpret(env); } catch (InterpreterException ie) { Console.WriteLine("Error: " + ie.Message + " on line: " + ie.T.LineNumber); #if DEBUG Console.ReadLine(); #endif Environment.Exit(1); } catch (Exception ex) { Console.WriteLine("Error: The interpreter encountered a problem: " + ex.Message); #if DEBUG Console.WriteLine(ex.StackTrace); Console.ReadLine(); #endif Environment.Exit(1); } } else { Console.WriteLine("Syntax error on line: " + parser.HighestLine); #if DEBUG //dump the tokens for inspection foreach (Token t in tokens) { Console.WriteLine(t.ToString()); } Console.ReadLine(); #endif Environment.Exit(1); } #if DEBUG Console.ReadKey(); #endif }