private static MoLangParser DefaultFactory(ITokenIterator iterator) { var parser = new MoLangParser(iterator); parser.ExpressionTraverser.Visitors.Add(new MathOptimizationVisitor()); return(parser); }
public WhereNotTriviaIterator(ITokenIterator tokens) { this.tokens = tokens; if (Current is ITriviaToken) { Next(); } }
/// <summary> /// The current token is unexpected, report an error and consume it. /// </summary> public static TextSpan UnexpectedToken(this ITokenIterator tokens) { // TODO shouldn't we ignore or combine unexpected token errors until we parse something successfully? var span = tokens.Current.Span; tokens.Context.Diagnostics.Add(ParseError.UnexpectedToken(tokens.Context.File, span)); tokens.Next(); return(span); }
private static NamespaceName ParseImplicitNamespaceName(ITokenIterator <IEssentialToken> tokens) { NamespaceName name = NamespaceName.Global; foreach (var segment in tokens.Context.File.Reference.Namespace) { name = name.Qualify(segment); } return(name); }
private static RootName ParseImplicitNamespaceName(ITokenIterator tokens) { RootName name = GlobalNamespaceName.Instance; foreach (var segment in tokens.Context.File.Reference.Namespace) { name = name.Qualify(segment); } return(name); }
public static T?AcceptToken <T>(this ITokenIterator <IToken> tokens) where T : class, IToken { if (tokens.Current is T token) { tokens.Next(); return(token); } return(null); }
public static bool Accept <T>(this ITokenIterator <IToken> tokens) where T : class, IToken { if (tokens.Current is T) { tokens.Next(); return(true); } return(false); }
public static (TextSpan, IIdentifierToken) ExpectIdentifier(this ITokenIterator tokens) { if (tokens.Current is IIdentifierToken identifier) { tokens.Next(); return(identifier.Span, identifier); } tokens.Context.Diagnostics.Add( ParseError.MissingToken(tokens.Context.File, typeof(IIdentifierToken), tokens.Current)); return(new TextSpan(tokens.Current.Span.Start, 0), null); }
public LexResult(ITokenIterator iterator) { var tokens = new List <IToken>(); do { tokens.Add(iterator.Current); } while (iterator.Next()); File = iterator.Context.File; Tokens = tokens.ToFixedList(); Diagnostics = iterator.Context.Diagnostics.Build(); }
public CompilationUnitSyntax Parse(ITokenIterator tokens) { var implicitNamespaceName = ParseImplicitNamespaceName(tokens); var parser = new Parser(tokens, implicitNamespaceName); var usingDirectives = parser.ParseUsingDirectives(); var declarations = parser.ParseTopLevelDeclarations(); tokens.Required <IEndOfFileToken>(); return(new CompilationUnitSyntax(tokens.Context.File, implicitNamespaceName, usingDirectives, declarations, tokens.Context.Diagnostics.Build())); }
public static T RequiredToken <T>(this ITokenIterator <IToken> tokens) where T : IToken { if (tokens.Current is T token) { tokens.Next(); return(token); } tokens.Context.Diagnostics.Add( ParseError.MissingToken(tokens.Context.File, typeof(T), tokens.Current)); throw new ParseFailedException($"Requires {typeof(T).GetFriendlyName()}, found {tokens.Current.GetType().GetFriendlyName()}"); }
public static bool AtEnd <T>(this ITokenIterator tokens) where T : IToken { switch (tokens.Current) { case T _: case IEndOfFileToken _: return(true); default: return(false); } }
public static TextSpan Expect <T>(this ITokenIterator <IToken> tokens) where T : IToken { if (tokens.Current is T token) { tokens.Next(); return(token.Span); } tokens.Context.Diagnostics.Add( ParseError.MissingToken(tokens.Context.File, typeof(T), tokens.Current)); // An empty span at the current location return(new TextSpan(tokens.Current.Span.Start, 0)); }
public ICompilationUnitSyntax Parse(ITokenIterator <IEssentialToken> tokens) { var implicitNamespaceName = ParseImplicitNamespaceName(tokens); var parser = new Parser(tokens, implicitNamespaceName); var usingDirectives = parser.ParseUsingDirectives(); var declarations = parser.ParseNonMemberDeclarations <IEndOfFileToken>(); var eof = tokens.Required <IEndOfFileToken>(); var span = TextSpan.FromStartEnd(0, eof.End); var diagnostics = tokens.Context.Diagnostics; var compilationUnit = new CompilationUnitSyntax(implicitNamespaceName, span, tokens.Context.File, usingDirectives, declarations); CheckSyntax(compilationUnit, diagnostics); compilationUnit.Attach(diagnostics.Build()); return(compilationUnit); }
public MoLangParser(ITokenIterator iterator) { _tokenIterator = iterator; ExpressionTraverser = new ExpressionTraverser(); }
public ModifierParser(ITokenIterator <IEssentialToken> tokens) : base(tokens) { }
public static ITokenIterator <IEssentialToken> WhereNotTrivia(this ITokenIterator <IToken> tokens) { return(new WhereNotTriviaIterator(tokens)); }
public static ITokenIterator WhereNotTrivia(this ITokenIterator tokens) { return(new WhereNotTriviaIterator(tokens)); }
public Parser(ITokenIterator tokens, RootName nameContext) { File = tokens.Context.File; Tokens = tokens; this.nameContext = nameContext; }
public static (T?, TextSpan) ExpectToken <T>(this ITokenIterator <IToken> tokens)
public Parser(ITokenIterator <IEssentialToken> tokens, NamespaceName?containingNamespace) : base(tokens) { this.containingNamespace = containingNamespace; }
public RecursiveDescentParser(ITokenIterator <IEssentialToken> tokens) { File = tokens.Context.File; Tokens = tokens; }