-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libnixf: generate token spelling from python script (#648)
- Loading branch information
Showing
13 changed files
with
203 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from tokens import bin_op_tokens, keyword_tokens, tokens | ||
|
||
|
||
def generate_token_section(section_name: str, tokens: list) -> str: | ||
if not tokens: | ||
return "" | ||
|
||
section = [f"#ifdef {section_name}"] | ||
section.extend(f"{section_name}({token.name})" for token in tokens) | ||
section.append(f"#endif // {section_name}\n") | ||
|
||
return "\n".join(section) | ||
|
||
|
||
def generate_token_kinds_inc() -> str: | ||
sections = [ | ||
generate_token_section("TOK_KEYWORD", keyword_tokens), | ||
generate_token_section("TOK", tokens), | ||
generate_token_section("TOK_BIN_OP", bin_op_tokens), | ||
] | ||
|
||
return "\n".join(filter(None, sections)).strip() | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
with open(sys.argv[1], "w") as f: | ||
f.write(generate_token_kinds_inc()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import tokens | ||
|
||
|
||
def tok_id(tok: tokens.Token): | ||
prefix = "tok" | ||
if isinstance(tok, tokens.KwToken): | ||
return f"{prefix}_kw_{tok.name}" | ||
if isinstance(tok, tokens.OpToken): | ||
return f"{prefix}_op_{tok.name}" | ||
return f"{prefix}_{tok.name}" | ||
|
||
|
||
def generate_tokens_h() -> str: | ||
header = """#pragma once | ||
#include <string_view> | ||
namespace nixf::tok { | ||
enum TokenKind { | ||
""" | ||
for token in tokens.tokens: | ||
header += f" {tok_id(token)},\n" | ||
|
||
header += "};\n\n" | ||
|
||
header += """constexpr std::string_view spelling(int Kind) { | ||
using namespace std::literals; | ||
switch (Kind) { | ||
""" | ||
|
||
for token in tokens.tokens: | ||
header += f' case {tok_id(token)}: return R"({token.spelling})"sv;\n' | ||
|
||
header += """ default: return ""sv; | ||
} | ||
} | ||
""" | ||
|
||
header += "} // namespace nixf::tok" | ||
|
||
return header | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
with open(sys.argv[1], "w") as f: | ||
f.write(generate_tokens_h()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
|
||
@dataclass | ||
class Token: | ||
name: str | ||
spelling: str | ||
|
||
|
||
class KwToken(Token): | ||
def __init__(self, name): | ||
self.name = name | ||
self.spelling = name | ||
|
||
|
||
keyword_tokens: List[Token] = [ | ||
KwToken("if"), | ||
KwToken("then"), | ||
KwToken("else"), | ||
KwToken("assert"), | ||
KwToken("with"), | ||
KwToken("let"), | ||
KwToken("in"), | ||
KwToken("rec"), | ||
KwToken("inherit"), | ||
KwToken("or"), | ||
] | ||
|
||
|
||
class OpToken(Token): | ||
pass | ||
|
||
|
||
bin_op_tokens: List[Token] = [ | ||
OpToken("not", "!"), | ||
OpToken("impl", "->"), | ||
OpToken("or", "||"), | ||
OpToken("and", "&&"), | ||
OpToken("eq", "=="), | ||
OpToken("neq", "!="), | ||
OpToken("lt", "<"), | ||
OpToken("gt", ">"), | ||
OpToken("le", "<="), | ||
OpToken("ge", ">="), | ||
OpToken("update", "//"), | ||
OpToken("add", "+"), | ||
OpToken("negate", "-"), | ||
OpToken("mul", "*"), | ||
OpToken("div", "/"), | ||
OpToken("concat", "++"), | ||
OpToken("pipe_into", "|>"), | ||
OpToken("pipe_from", "<|"), | ||
] | ||
|
||
tokens: List[Token] = [ | ||
*keyword_tokens, | ||
Token("eof", "eof"), | ||
Token("id", "id"), | ||
Token("int", "int"), | ||
Token("float", "float"), | ||
Token("dquote", '"'), | ||
Token("string_part", "string_part"), | ||
Token("string_escape", "string_escape"), | ||
Token("quote2", "''"), | ||
Token("path_fragment", "path_fragment"), | ||
Token("spath", "<path>"), | ||
Token("uri", "uri"), | ||
Token("r_curly", "}"), | ||
Token("dollar_curly", "${"), | ||
Token("ellipsis", "..."), | ||
Token("comma", ","), | ||
Token("dot", "."), | ||
Token("semi_colon", ";"), | ||
Token("eq", "="), | ||
Token("l_curly", "{"), | ||
Token("l_paren", "("), | ||
Token("r_paren", ")"), | ||
Token("l_bracket", "["), | ||
Token("r_bracket", "]"), | ||
Token("question", "?"), | ||
Token("at", "@"), | ||
Token("colon", ":"), | ||
Token("unknown", "unknown"), | ||
Token("path_end", "path_end"), | ||
*bin_op_tokens, | ||
] |
Oops, something went wrong.