Skip to content

Commit

Permalink
Move GetTokenTypeName to Scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
MerijnHendriks committed Apr 9, 2024
1 parent c136af6 commit f4a320b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 73 deletions.
87 changes: 73 additions & 14 deletions Lox.Compiler/Lexing/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,79 @@ public Scanner(Logger logger, Pattern[] patterns)
this._patterns = patterns;
}

private string GetTokenName(int type)
{
switch (type)
{
case TokenType.END_OF_FILE: return "EOF";
case TokenType.END_OF_LINE: return "EOL";
case TokenType.DOT: return ".";
case TokenType.COMMA: return ",";
case TokenType.SEMICOLON: return ";";
case TokenType.LEFT_CURLY: return "{";
case TokenType.RIGHT_CURLY: return "}";
case TokenType.LEFT_CIRCLE: return "(";
case TokenType.RIGHT_CIRCLE: return ")";
case TokenType.LEFT_ARROW: return "<";
case TokenType.RIGHT_ARROW: return ">";
case TokenType.ASSIGN: return "=";
case TokenType.NOT: return "!";
case TokenType.PLUS: return "+";
case TokenType.MINUS: return "-";
case TokenType.STAR: return "*";
case TokenType.SLASH: return "/";
case TokenType.EQUAL: return "==";
case TokenType.NOT_EQUAL: return "!=";
case TokenType.LESS_EQUAL: return "<=";
case TokenType.GREATER_EQUAL: return ">=";
case TokenType.ADD_ASSIGN: return "+=";
case TokenType.SUBSTRACT_ASSIGN: return "-=";
case TokenType.MULTIPLY_ASSIGN: return "*=";
case TokenType.DIVIDE_ASSIGN: return "/=";
case TokenType.IDENTIFIER: return "IDENTIFIER";
case TokenType.NUMBER: return "NUMBER";
case TokenType.STRING: return "STRING";
case TokenType.TRUE: return "true";
case TokenType.FALSE: return "false";
case TokenType.NIL: return "nil";
case TokenType.THIS: return "this";
case TokenType.SUPER: return "super";
case TokenType.VAR: return "var";
case TokenType.FUN: return "fun";
case TokenType.CLASS: return "class";
case TokenType.AND: return "and";
case TokenType.OR: return "or";
case TokenType.IF: return "if";
case TokenType.ELSE: return "else";
case TokenType.WHILE: return "while";
case TokenType.FOR: return "for";
case TokenType.CONTINUE: return "continue";
case TokenType.BREAK: return "break";
case TokenType.RETURN: return "return";
case TokenType.PRINT: return "print";
case TokenType.LINE_COMMENT: return "//";
case TokenType.TAB: return "\t";
case TokenType.WHITESPACE: return "' '";
case TokenType.INVALID:
default:
throw new Exception("Invalid type.");
}
}

private void PrintToken(Token token, SourcePosition current)
{
string format = "[{0}, {1}, {2}] {3}";
string typeName = _textHelper.GetTokenName(token.Type);

Check failure on line 82 in Lox.Compiler/Lexing/Scanner.cs

View workflow job for this annotation

GitHub Actions / build

'TextHelper' does not contain a definition for 'GetTokenName' and no accessible extension method 'GetTokenName' accepting a first argument of type 'TextHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 82 in Lox.Compiler/Lexing/Scanner.cs

View workflow job for this annotation

GitHub Actions / build

'TextHelper' does not contain a definition for 'GetTokenName' and no accessible extension method 'GetTokenName' accepting a first argument of type 'TextHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 82 in Lox.Compiler/Lexing/Scanner.cs

View workflow job for this annotation

GitHub Actions / build

'TextHelper' does not contain a definition for 'GetTokenName' and no accessible extension method 'GetTokenName' accepting a first argument of type 'TextHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 82 in Lox.Compiler/Lexing/Scanner.cs

View workflow job for this annotation

GitHub Actions / build

'TextHelper' does not contain a definition for 'GetTokenName' and no accessible extension method 'GetTokenName' accepting a first argument of type 'TextHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 82 in Lox.Compiler/Lexing/Scanner.cs

View workflow job for this annotation

GitHub Actions / build

'TextHelper' does not contain a definition for 'GetTokenName' and no accessible extension method 'GetTokenName' accepting a first argument of type 'TextHelper' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 82 in Lox.Compiler/Lexing/Scanner.cs

View workflow job for this annotation

GitHub Actions / build

'TextHelper' does not contain a definition for 'GetTokenName' and no accessible extension method 'GetTokenName' accepting a first argument of type 'TextHelper' could be found (are you missing a using directive or an assembly reference?)
string formatted = string.Format(format, current.Index, current.Line, current.Column, typeName);

if (!string.IsNullOrEmpty(token.Value))
{
formatted += (", " + token.Value);
}

_logger.WriteInfo(formatted);
}

private SourcePosition ScanToken(string file, string source, SourcePosition current, ref Token token)
{
for (int i = 0; i < this._patterns.Length; ++i)
Expand All @@ -36,20 +109,6 @@ private SourcePosition ScanToken(string file, string source, SourcePosition curr
throw new PatternMatchingException(error);
}

private void PrintToken(Token token, SourcePosition current)
{
string format = "[{0}, {1}, {2}] {3}";
string typeName = _textHelper.GetTokenTypeName(token.Type);
string formatted = string.Format(format, current.Index, current.Line, current.Column, typeName);

if (!string.IsNullOrEmpty(token.Value))
{
formatted += (", " + token.Value);
}

_logger.WriteInfo(formatted);
}

public ScanResult Run(string file, string source)
{
Token token = new Token();
Expand Down
59 changes: 0 additions & 59 deletions Lox.Compiler/Lexing/TextHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,64 +107,5 @@ public bool IsIdentifier(string source, int index)
|| this.IsAlphaLower(source, index)
|| this.IsDigit(source, index);
}

public string GetTokenTypeName(int type)
{
switch (type)
{
case TokenType.END_OF_FILE: return "EOF";
case TokenType.END_OF_LINE: return "EOL";
case TokenType.DOT: return ".";
case TokenType.COMMA: return ",";
case TokenType.SEMICOLON: return ";";
case TokenType.LEFT_CURLY: return "{";
case TokenType.RIGHT_CURLY: return "}";
case TokenType.LEFT_CIRCLE: return "(";
case TokenType.RIGHT_CIRCLE: return ")";
case TokenType.LEFT_ARROW: return "<";
case TokenType.RIGHT_ARROW: return ">";
case TokenType.ASSIGN: return "=";
case TokenType.NOT: return "!";
case TokenType.PLUS: return "+";
case TokenType.MINUS: return "-";
case TokenType.STAR: return "*";
case TokenType.SLASH: return "/";
case TokenType.EQUAL: return "==";
case TokenType.NOT_EQUAL: return "!=";
case TokenType.LESS_EQUAL: return "<=";
case TokenType.GREATER_EQUAL: return ">=";
case TokenType.ADD_ASSIGN: return "+=";
case TokenType.SUBSTRACT_ASSIGN: return "-=";
case TokenType.MULTIPLY_ASSIGN: return "*=";
case TokenType.DIVIDE_ASSIGN: return "/=";
case TokenType.IDENTIFIER: return "IDENTIFIER";
case TokenType.NUMBER: return "NUMBER";
case TokenType.STRING: return "STRING";
case TokenType.TRUE: return "true";
case TokenType.FALSE: return "false";
case TokenType.NIL: return "nil";
case TokenType.THIS: return "this";
case TokenType.SUPER: return "super";
case TokenType.VAR: return "var";
case TokenType.FUN: return "fun";
case TokenType.CLASS: return "class";
case TokenType.AND: return "and";
case TokenType.OR: return "or";
case TokenType.IF: return "if";
case TokenType.ELSE: return "else";
case TokenType.WHILE: return "while";
case TokenType.FOR: return "for";
case TokenType.CONTINUE: return "continue";
case TokenType.BREAK: return "break";
case TokenType.RETURN: return "return";
case TokenType.PRINT: return "print";
case TokenType.LINE_COMMENT: return "//";
case TokenType.TAB: return "\t";
case TokenType.WHITESPACE: return "' '";
case TokenType.INVALID:
default:
throw new Exception("Invalid type.");
}
}
}
}

0 comments on commit f4a320b

Please sign in to comment.