/// <summary> /// Visits the syntax node. /// </summary> /// <param name="parentNode">Node</param> /// <param name="typeIdentifier">Type identifier</param> /// <param name="identifier">Identifier</param> /// <param name="isModel">Is model</param> /// <param name="accMod">Access modifier</param> /// <param name="inhMod">Inheritance modifier</param> /// <param name="isAsync">Is async</param> internal void Visit(MachineDeclaration parentNode, Token typeIdentifier, Token identifier, bool isModel, AccessModifier accMod, InheritanceModifier inhMod, bool isAsync) { if (parentNode.IsModel) { isModel = true; } var node = new MethodDeclaration(base.TokenStream.Program, isModel); node.AccessModifier = accMod; node.InheritanceModifier = inhMod; node.TypeIdentifier = typeIdentifier; node.Identifier = identifier; node.IsAsync = isAsync; node.LeftParenthesisToken = base.TokenStream.Peek(); base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); while (!base.TokenStream.Done && base.TokenStream.Peek().Type != TokenType.RightParenthesis) { base.TokenStream.Swap(new Token(base.TokenStream.Peek().TextUnit)); node.Parameters.Add(base.TokenStream.Peek()); base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); } node.RightParenthesisToken = base.TokenStream.Peek(); base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); if (base.TokenStream.Done || (base.TokenStream.Peek().Type != TokenType.LeftCurlyBracket && base.TokenStream.Peek().Type != TokenType.Semicolon)) { throw new ParsingException("Expected \"{\" or \";\".", new List<TokenType> { TokenType.LeftCurlyBracket, TokenType.Semicolon }); } if (base.TokenStream.Peek().Type == TokenType.LeftCurlyBracket) { var blockNode = new BlockSyntax(base.TokenStream.Program, parentNode, null, parentNode.IsModel); new BlockSyntaxVisitor(base.TokenStream).Visit(blockNode); node.StatementBlock = blockNode; } else if (base.TokenStream.Peek().Type == TokenType.Semicolon) { node.SemicolonToken = base.TokenStream.Peek(); } parentNode.MethodDeclarations.Add(node); }
/// <summary> /// Visits the syntax node. /// </summary> /// <param name="parentNode">Node</param> /// <param name="isModel">Is model</param> /// <param name="accMod">Access modifier</param> /// <param name="inhMod">Inheritance modifier</param> /// <param name="isAsync">Is async</param> internal void Visit(MachineDeclaration parentNode, bool isModel, AccessModifier accMod, InheritanceModifier inhMod, bool isAsync) { TextUnit textUnit = null; new TypeIdentifierVisitor(base.TokenStream).Visit(ref textUnit); var typeIdentifier = new Token(textUnit, TokenType.TypeIdentifier); if (base.TokenStream.Done || base.TokenStream.Peek().Type != TokenType.Identifier) { throw new ParsingException("Expected field or method identifier.", new List<TokenType> { TokenType.Identifier }); } var identifierToken = base.TokenStream.Peek(); base.TokenStream.Index++; base.TokenStream.SkipWhiteSpaceAndCommentTokens(); if (base.TokenStream.Done || (base.TokenStream.Peek().Type != TokenType.LeftParenthesis && base.TokenStream.Peek().Type != TokenType.Semicolon)) { throw new ParsingException("Expected \"(\" or \";\".", new List<TokenType> { TokenType.LeftParenthesis, TokenType.Semicolon }); } if (base.TokenStream.Peek().Type == TokenType.LeftParenthesis) { new MethodDeclarationVisitor(base.TokenStream).Visit(parentNode, typeIdentifier, identifierToken, isModel, accMod, inhMod, isAsync); } else if (base.TokenStream.Peek().Type == TokenType.Semicolon) { if (inhMod == InheritanceModifier.Abstract) { throw new ParsingException("A field cannot be abstract.", new List<TokenType>()); } else if (inhMod == InheritanceModifier.Virtual) { throw new ParsingException("A field cannot be virtual.", new List<TokenType>()); } else if (inhMod == InheritanceModifier.Override) { throw new ParsingException("A field cannot be overriden.", new List<TokenType>()); } if (isModel) { throw new ParsingException("A field cannot be a model.", new List<TokenType>()); } if (isAsync) { throw new ParsingException("A field cannot be async.", new List<TokenType>()); } var node = new FieldDeclaration(base.TokenStream.Program, parentNode, parentNode.IsModel); node.AccessModifier = accMod; node.TypeIdentifier = typeIdentifier; node.Identifier = identifierToken; node.SemicolonToken = base.TokenStream.Peek(); parentNode.FieldDeclarations.Add(node); } }
/// <summary> /// Swaps the current token with the new token. Does nothing if the stream is /// empty. /// </summary> public void Swap(Token token) { if (this.Index == this.Tokens.Count) { return; } this.Tokens[this.Index] = token; }