public LexTokenList Process() { LexTokenList lexTokens = new LexTokenList(); foreach (LexToken lexToken in this.LexTokens) { if (lexToken.Kind != LexKind.Comment) { lexTokens.Add(lexToken); } } return(lexTokens); }
static void Main(string[] args) { #if debugenabled try { #endif string Code = File.ReadAllText("Tests\\Ackermann.cs"); CodeLexer lexer = new CodeLexer(Code); LexTokenList lexTokens = lexer.Analyze(); CPreprocessor preProcessor = new CPreprocessor(lexTokens); lexTokens = preProcessor.Process(); Console.WriteLine("LexTokens (post-processed):"); foreach (LexToken lexToken in lexTokens) { Console.WriteLine(lexToken.Kind.ToString() + " " + (lexToken.Value == null ? "" : lexToken.Value.ToString())); } Console.WriteLine("------\nGenerating AST.."); AstGenerator astGenerator = new AstGenerator(lexTokens); StatementList statements = astGenerator.Generate(); Console.WriteLine("Generated AST!"); Console.WriteLine(View.ASTView.GenerateStatements(statements)); #if debugenabled } catch (Exception ex) { Console.WriteLine(ex.Message + "\n" + ex.StackTrace); } #endif Console.ReadLine(); }
public LexTokenList Analyze() { LexTokenList LexTokens = new LexTokenList(); StringBuilder sb = new StringBuilder(); int Line = 1; for (int i = 0; i < Input.Length; i++) { LexKind kind = LexKind.Terminal; string value = ""; switch (Input[i]) { case '(': kind = LexKind.ParentheseOpen; break; case ')': kind = LexKind.ParentheseClose; break; case '[': kind = LexKind.BracketOpen; break; case ']': kind = LexKind.BracketClose; break; case '{': kind = LexKind.BraceOpen; break; case '}': kind = LexKind.BraceClose; break; case '<': kind = LexKind.ChevronOpen; break; case '>': kind = LexKind.ChevronClose; break; case ':': kind = LexKind.Colon; break; case ';': kind = LexKind.Semicolon; break; case ',': kind = LexKind.Comma; break; case '"': { i++; while (Input[i] != '"') { if (Input[i] == '\\' && Input[i + 1] == '"') { i++; } sb.Append(Input[i]); i++; } value = sb.ToString(); kind = LexKind.String; sb.Clear(); break; } case '=': kind = LexKind.Equals; break; case ' ': // Discard case '\r': case '\t': break; case '\n': Line++; break; case '?': { kind = LexKind.Question; break; } case '!': { kind = LexKind.Exclamation; break; } case '+': { kind = LexKind.Add; break; } case '-': { kind = LexKind.Sub; break; } case '*': { kind = LexKind.Mul; break; } case '/': { if (Input.Length > i + 1 && Input[i + 1] == '/') { i += 2; for (; Input[i] != '\n' && i < Input.Length; i++) { sb.Append(Input[i]); } value = sb.ToString(); kind = LexKind.Comment; sb.Clear(); Line++; } else { kind = LexKind.Div; } break; } default: { if (Char.IsLetterOrDigit(Input[i]) || Input[i] == '.') { while (Input.Length > i && (Char.IsLetterOrDigit(Input[i]) || Input[i] == '.')) { sb.Append(Input[i++]); } i--; } value = sb.ToString(); kind = Identify(value); sb.Clear(); break; } } if (kind != LexKind.Terminal) { LexTokens.Add(new LexToken() { Kind = kind, Value = value, Line = Line }); } } LexTokens.Add(new LexToken() { Kind = LexKind.Terminal }); LexTokens.Add(new LexToken() { Kind = LexKind.EOF }); return(LexTokens); }
/// <summary> /// <inheritdoc/> /// </summary> /// <param name="lexTokens"></param> public TokenReader(LexTokenList lexTokens) { this.LexTokens = lexTokens; this.Base = 0; }
/// <summary> /// <inheritdoc/> /// </summary> /// <param name="lexTokens"></param> public AstGenerator(LexTokenList lexTokens) { this.tokenReader = new TokenReader(lexTokens); }
public CPreprocessor(LexTokenList lexTokens) { this.LexTokens = lexTokens; }