private static Double?StringToNumber(String str) { try { var lexer = new GLuaLexer(str); foreach (LToken token in lexer.Lex( )) { return(( Double )token.Value); } return(null); } catch (Exception) { return(null); } }
public static void LexFile(String filePath, String outputPath = "stdin") { if (!File.Exists(filePath)) { throw new Exception("File doesn't exists."); } StreamWriter outputWriter; using (outputWriter = outputPath == "stdin" ? new StreamWriter(Console.OpenStandardOutput( ), Encoding.UTF8, 4096) : new StreamWriter(outputPath, false, Encoding.UTF8, 4096)) using (FileStream handle = File.OpenRead(filePath)) { var max = new SourceLocation(Int32.MaxValue, Int32.MaxValue, Int32.MaxValue); var last = new SourceRange(max, max); var sw = Stopwatch.StartNew( ); var lexer = new GLuaLexer(handle); foreach (LToken token in lexer.Lex( )) { outputWriter.WriteLine($"LToken ( '{token.ID}', '{token.Raw}', '{token.Value}', {token.Type}, {token.Range} )"); foreach (Token flair in token.LeadingFlair) { outputWriter.WriteLine($"\tLTokenFlair ( '{flair.ID}', '{flair.Raw}', '{flair.Value}', {flair.Type}, {flair.Range} )"); } if (last == token.Range && token.Type != TokenType.EOF) { throw new Exception("Possible infinite loop. Token has same range as last."); } last = token.Range; } sw.Stop( ); outputWriter.WriteLine($"Time elapsed on lexing: {HumanTime ( sw )}"); } }