/// <summary> /// Constructs the parser /// </summary> /// <param name="file">The file name to parse</param> public Parser(string file) { lexer = new Lexer(File.ReadAllText(file), this); Errors = new List<string>(); Warnings = new List<string>(); CurrentScope = Global; int pos = file.LastIndexOf('.'); string realname = pos >= 0 ? file.Substring(0, pos) : file; Output.Add(LogType.Parse, File.CreateText(realname + "_parseinfo.txt")); Output.Add(LogType.SymbolTable, File.CreateText(realname + "_symboltable.txt")); Output.Add(LogType.Error, System.Console.Error); Output.Add(LogType.Warning, System.Console.Error); Output.Add(LogType.Code, File.CreateText(realname + ".m")); }
/// <summary> /// Tries to consume a token of the given type /// </summary> /// <param name="val">The value of the token, can be null to ignore</param> /// <param name="type">The type of the token, can be null to ignore</param> /// <returns>A bool signifying if the symbol could be matched or not</returns> protected bool Match(int p, string val, Lexer.Token.TokenType? type) { bool ok = true; if (val != null && lookahead.Value != val) ok = false; else if (type.HasValue && lookahead.Type != type.Value) ok = false; if (!ok && p == 1) SyntaxError(string.Format("{0}: expected '{1}', got instead '{2}', a {3}", lookahead.Line, val ?? type.Value.ToString(), lookahead.Value, lookahead.Type.ToString())); if (lookahead.Type != Lexer.Token.TokenType.EndOfFile) lookahead = lexer.GetNextToken(); return ok; }
/// <summary> /// Tries to match a token from a type /// </summary> /// <param name="type">The type of the token to match against</param> /// <returns></returns> protected bool Match(int p, Lexer.Token.TokenType type) { return Match(p, null, type); }