public static bool TryGetExpressionNode(IParser parser, out StringExpressionNode node) { bool result = false; node = null; if (parser.PeekToken(TokenCategory.StringLiteral) || parser.PeekToken(TokenCategory.CharacterLiteral) || parser.PeekToken(TokenCategory.IncompleteMultiLineStringLiteral)) { parser.NextToken(); node = new StringExpressionNode(parser.Token); node.StartIndex = parser.Token.Span.Start; node.EndIndex = parser.Token.Span.End; result = true; } return(result); }
public static bool TryParseNode(GeneroParser parser, out PreprocessorNode node) { node = null; bool result = false; if (parser.PeekToken(TokenKind.Ampersand)) { var options = parser.Tokenizer.CurrentOptions; // backup the current parser options var newOptions = options | TokenizerOptions.VerbatimCommentsAndLineJoins; // allow us to continue until the newline parser.Tokenizer.AdjustOptions(newOptions); parser.NextToken(); result = true; node = new PreprocessorNode(); node.StartIndex = parser.Token.Span.Start; StringBuilder sb = new StringBuilder(); switch (parser.PeekToken().Kind) { case TokenKind.IncludeKeyword: parser.NextToken(); node.Type = PreprocessorType.Include; StringExpressionNode strExpr; if (StringExpressionNode.TryGetExpressionNode(parser, out strExpr)) { node.IncludeFile = strExpr.LiteralValue; } else { parser.ReportSyntaxError("Invalid include file preprocessor directive found."); } break; case TokenKind.DefineKeyword: parser.NextToken(); node.Type = PreprocessorType.Define; break; case TokenKind.UndefKeyword: parser.NextToken(); node.Type = PreprocessorType.Undef; break; case TokenKind.IfdefKeyword: parser.NextToken(); node.Type = PreprocessorType.Ifdef; break; case TokenKind.ElseKeyword: parser.NextToken(); node.Type = PreprocessorType.Else; break; case TokenKind.EndifKeyword: parser.NextToken(); node.Type = PreprocessorType.Endif; break; default: break; } while (!parser.PeekToken(TokenKind.NewLine) && !parser.PeekToken(TokenKind.EndOfFile)) { node.PreprocessorTokens.Add(parser.NextToken()); } parser.Tokenizer.AdjustOptions(options); // restore the backed up parser options while (parser.PeekToken(TokenKind.NewLine)) { parser.NextToken(); } node.IsComplete = true; } return(result); }