feat(cli): highlight midas keywords
This commit is contained in:
@@ -50,4 +50,8 @@ span {
|
||||
--border: 2px;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
&.keyword {
|
||||
color: rgb(211, 72, 9);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
import ast
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, TextIO
|
||||
|
||||
import click
|
||||
|
||||
from midas.ast.location import Location
|
||||
import midas.ast.midas as m
|
||||
import midas.ast.python as p
|
||||
from midas.ast.location import Location
|
||||
from midas.ast.printer import PythonAstPrinter
|
||||
from midas.cli.highlighter import Highlighter, MidasHighlighter, PythonHighlighter
|
||||
from midas.lexer.midas import MidasLexer
|
||||
@@ -73,20 +74,24 @@ def highlight_midas(source: str, path: str) -> Highlighter:
|
||||
parser = MidasParser(tokens)
|
||||
stmts: list[m.Stmt] = parser.parse()
|
||||
highlighter = MidasHighlighter(source)
|
||||
for err in parser.errors:
|
||||
print(err.get_report())
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LocatableToken:
|
||||
def __init__(self, token: Token):
|
||||
self.token: Token = token
|
||||
token: Token
|
||||
|
||||
@property
|
||||
def location(self) -> Location:
|
||||
return self.token.get_location()
|
||||
|
||||
for stmt in stmts:
|
||||
highlighter.highlight(stmt)
|
||||
for token in tokens:
|
||||
if token.type == TokenType.COMMENT:
|
||||
highlighter.wrap(LocatableToken(token), "comment")
|
||||
for stmt in stmts:
|
||||
highlighter.highlight(stmt)
|
||||
elif token.is_keyword:
|
||||
highlighter.wrap(LocatableToken(token), "keyword")
|
||||
return highlighter
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
from midas.lexer.token import TokenType
|
||||
|
||||
KEYWORDS: dict[str, TokenType] = {
|
||||
"type": TokenType.TYPE,
|
||||
"op": TokenType.OP,
|
||||
"predicate": TokenType.PREDICATE,
|
||||
"extend": TokenType.EXTEND,
|
||||
"where": TokenType.WHERE,
|
||||
"true": TokenType.TRUE,
|
||||
"false": TokenType.FALSE,
|
||||
"none": TokenType.NONE,
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
from midas.lexer.base import Lexer
|
||||
from midas.lexer.keyword import KEYWORDS
|
||||
from midas.lexer.token import TokenType
|
||||
from midas.lexer.token import KEYWORDS, TokenType
|
||||
|
||||
|
||||
class MidasLexer(Lexer):
|
||||
|
||||
@@ -58,6 +58,18 @@ class TokenType(Enum):
|
||||
NEWLINE = auto()
|
||||
|
||||
|
||||
KEYWORDS: dict[str, TokenType] = {
|
||||
"type": TokenType.TYPE,
|
||||
"op": TokenType.OP,
|
||||
"predicate": TokenType.PREDICATE,
|
||||
"extend": TokenType.EXTEND,
|
||||
"where": TokenType.WHERE,
|
||||
"true": TokenType.TRUE,
|
||||
"false": TokenType.FALSE,
|
||||
"none": TokenType.NONE,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Token:
|
||||
"""A scanned token"""
|
||||
@@ -86,3 +98,7 @@ class Token:
|
||||
|
||||
def location_to(self, to: Token) -> Location:
|
||||
return Location.span(self.get_location(), to.get_location())
|
||||
|
||||
@property
|
||||
def is_keyword(self) -> bool:
|
||||
return self.lexeme in KEYWORDS
|
||||
|
||||
Reference in New Issue
Block a user