2022-11-03 01:31:26 -03:00
|
|
|
# Parser for C code
|
|
|
|
# Originally by Mark Shannon (mark@hotpy.org)
|
|
|
|
# https://gist.github.com/markshannon/db7ab649440b5af765451bb77c7dba34
|
|
|
|
|
|
|
|
import re
|
|
|
|
from dataclasses import dataclass
|
2023-08-18 10:42:45 -03:00
|
|
|
from collections.abc import Iterator
|
2022-11-03 01:31:26 -03:00
|
|
|
|
2023-08-18 10:42:45 -03:00
|
|
|
|
|
|
|
def choice(*opts: str) -> str:
|
2022-11-03 01:31:26 -03:00
|
|
|
return "|".join("(%s)" % opt for opt in opts)
|
|
|
|
|
2023-08-18 10:42:45 -03:00
|
|
|
|
2022-11-03 01:31:26 -03:00
|
|
|
# Regexes
|
|
|
|
|
|
|
|
# Longer operators must go before shorter ones.
|
|
|
|
|
2023-08-18 10:42:45 -03:00
|
|
|
PLUSPLUS = r"\+\+"
|
|
|
|
MINUSMINUS = r"--"
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
# ->
|
2023-08-18 10:42:45 -03:00
|
|
|
ARROW = r"->"
|
|
|
|
ELLIPSIS = r"\.\.\."
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
# Assignment operators
|
2023-08-18 10:42:45 -03:00
|
|
|
TIMESEQUAL = r"\*="
|
|
|
|
DIVEQUAL = r"/="
|
|
|
|
MODEQUAL = r"%="
|
|
|
|
PLUSEQUAL = r"\+="
|
|
|
|
MINUSEQUAL = r"-="
|
|
|
|
LSHIFTEQUAL = r"<<="
|
|
|
|
RSHIFTEQUAL = r">>="
|
|
|
|
ANDEQUAL = r"&="
|
|
|
|
OREQUAL = r"\|="
|
|
|
|
XOREQUAL = r"\^="
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
# Operators
|
2023-08-18 10:42:45 -03:00
|
|
|
PLUS = r"\+"
|
|
|
|
MINUS = r"-"
|
|
|
|
TIMES = r"\*"
|
|
|
|
DIVIDE = r"/"
|
|
|
|
MOD = r"%"
|
|
|
|
NOT = r"~"
|
|
|
|
XOR = r"\^"
|
|
|
|
LOR = r"\|\|"
|
|
|
|
LAND = r"&&"
|
|
|
|
LSHIFT = r"<<"
|
|
|
|
RSHIFT = r">>"
|
|
|
|
LE = r"<="
|
|
|
|
GE = r">="
|
|
|
|
EQ = r"=="
|
|
|
|
NE = r"!="
|
|
|
|
LT = r"<"
|
|
|
|
GT = r">"
|
|
|
|
LNOT = r"!"
|
|
|
|
OR = r"\|"
|
|
|
|
AND = r"&"
|
|
|
|
EQUALS = r"="
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
# ?
|
2023-08-18 10:42:45 -03:00
|
|
|
CONDOP = r"\?"
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
# Delimiters
|
2023-08-18 10:42:45 -03:00
|
|
|
LPAREN = r"\("
|
|
|
|
RPAREN = r"\)"
|
|
|
|
LBRACKET = r"\["
|
|
|
|
RBRACKET = r"\]"
|
|
|
|
LBRACE = r"\{"
|
|
|
|
RBRACE = r"\}"
|
|
|
|
COMMA = r","
|
|
|
|
PERIOD = r"\."
|
|
|
|
SEMI = r";"
|
|
|
|
COLON = r":"
|
|
|
|
BACKSLASH = r"\\"
|
|
|
|
|
|
|
|
operators = {op: pattern for op, pattern in globals().items() if op == op.upper()}
|
2022-11-03 01:31:26 -03:00
|
|
|
for op in operators:
|
|
|
|
globals()[op] = op
|
2023-08-18 10:42:45 -03:00
|
|
|
opmap = {pattern.replace("\\", "") or "\\": op for op, pattern in operators.items()}
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
# Macros
|
2024-10-07 10:56:39 -03:00
|
|
|
macro = r"#.*\n"
|
2023-11-07 05:42:39 -04:00
|
|
|
CMACRO = "CMACRO"
|
2022-11-03 01:31:26 -03:00
|
|
|
|
2023-08-18 10:42:45 -03:00
|
|
|
id_re = r"[a-zA-Z_][0-9a-zA-Z_]*"
|
|
|
|
IDENTIFIER = "IDENTIFIER"
|
2022-11-03 01:31:26 -03:00
|
|
|
|
2023-11-07 05:42:39 -04:00
|
|
|
|
2023-08-18 10:42:45 -03:00
|
|
|
suffix = r"([uU]?[lL]?[lL]?)"
|
|
|
|
octal = r"0[0-7]+" + suffix
|
|
|
|
hex = r"0[xX][0-9a-fA-F]+"
|
|
|
|
decimal_digits = r"(0|[1-9][0-9]*)"
|
2022-11-03 01:31:26 -03:00
|
|
|
decimal = decimal_digits + suffix
|
|
|
|
|
|
|
|
|
|
|
|
exponent = r"""([eE][-+]?[0-9]+)"""
|
|
|
|
fraction = r"""([0-9]*\.[0-9]+)|([0-9]+\.)"""
|
2023-08-18 10:42:45 -03:00
|
|
|
float = "((((" + fraction + ")" + exponent + "?)|([0-9]+" + exponent + "))[FfLl]?)"
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
number_re = choice(octal, hex, float, decimal)
|
2023-08-18 10:42:45 -03:00
|
|
|
NUMBER = "NUMBER"
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
simple_escape = r"""([a-zA-Z._~!=&\^\-\\?'"])"""
|
|
|
|
decimal_escape = r"""(\d+)"""
|
|
|
|
hex_escape = r"""(x[0-9a-fA-F]+)"""
|
2023-08-18 10:42:45 -03:00
|
|
|
escape_sequence = (
|
|
|
|
r"""(\\(""" + simple_escape + "|" + decimal_escape + "|" + hex_escape + "))"
|
|
|
|
)
|
|
|
|
string_char = r"""([^"\\\n]|""" + escape_sequence + ")"
|
|
|
|
str_re = '"' + string_char + '*"'
|
|
|
|
STRING = "STRING"
|
|
|
|
char = r"\'.\'" # TODO: escape sequence
|
|
|
|
CHARACTER = "CHARACTER"
|
2022-11-03 01:31:26 -03:00
|
|
|
|
2023-12-07 08:49:40 -04:00
|
|
|
comment_re = r"(//.*)|/\*([^*]|\*[^/])*\*/"
|
2023-08-18 10:42:45 -03:00
|
|
|
COMMENT = "COMMENT"
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
newline = r"\n"
|
2023-08-18 10:42:45 -03:00
|
|
|
invalid = (
|
|
|
|
r"\S" # A single non-space character that's not caught by any of the other patterns
|
2022-11-03 01:31:26 -03:00
|
|
|
)
|
2023-08-18 10:42:45 -03:00
|
|
|
matcher = re.compile(
|
|
|
|
choice(
|
|
|
|
id_re,
|
|
|
|
number_re,
|
|
|
|
str_re,
|
|
|
|
char,
|
|
|
|
newline,
|
|
|
|
macro,
|
|
|
|
comment_re,
|
|
|
|
*operators.values(),
|
|
|
|
invalid,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
letter = re.compile(r"[a-zA-Z_]")
|
|
|
|
|
|
|
|
|
|
|
|
kwds = []
|
|
|
|
AUTO = "AUTO"
|
|
|
|
kwds.append(AUTO)
|
|
|
|
BREAK = "BREAK"
|
|
|
|
kwds.append(BREAK)
|
|
|
|
CASE = "CASE"
|
|
|
|
kwds.append(CASE)
|
|
|
|
CHAR = "CHAR"
|
|
|
|
kwds.append(CHAR)
|
|
|
|
CONST = "CONST"
|
|
|
|
kwds.append(CONST)
|
|
|
|
CONTINUE = "CONTINUE"
|
|
|
|
kwds.append(CONTINUE)
|
|
|
|
DEFAULT = "DEFAULT"
|
|
|
|
kwds.append(DEFAULT)
|
|
|
|
DO = "DO"
|
|
|
|
kwds.append(DO)
|
|
|
|
DOUBLE = "DOUBLE"
|
|
|
|
kwds.append(DOUBLE)
|
|
|
|
ELSE = "ELSE"
|
|
|
|
kwds.append(ELSE)
|
|
|
|
ENUM = "ENUM"
|
|
|
|
kwds.append(ENUM)
|
|
|
|
EXTERN = "EXTERN"
|
|
|
|
kwds.append(EXTERN)
|
|
|
|
FLOAT = "FLOAT"
|
|
|
|
kwds.append(FLOAT)
|
|
|
|
FOR = "FOR"
|
|
|
|
kwds.append(FOR)
|
|
|
|
GOTO = "GOTO"
|
|
|
|
kwds.append(GOTO)
|
|
|
|
IF = "IF"
|
|
|
|
kwds.append(IF)
|
|
|
|
INLINE = "INLINE"
|
|
|
|
kwds.append(INLINE)
|
|
|
|
INT = "INT"
|
|
|
|
kwds.append(INT)
|
|
|
|
LONG = "LONG"
|
|
|
|
kwds.append(LONG)
|
|
|
|
OFFSETOF = "OFFSETOF"
|
|
|
|
kwds.append(OFFSETOF)
|
|
|
|
RESTRICT = "RESTRICT"
|
|
|
|
kwds.append(RESTRICT)
|
|
|
|
RETURN = "RETURN"
|
|
|
|
kwds.append(RETURN)
|
|
|
|
SHORT = "SHORT"
|
|
|
|
kwds.append(SHORT)
|
|
|
|
SIGNED = "SIGNED"
|
|
|
|
kwds.append(SIGNED)
|
|
|
|
SIZEOF = "SIZEOF"
|
|
|
|
kwds.append(SIZEOF)
|
|
|
|
STATIC = "STATIC"
|
|
|
|
kwds.append(STATIC)
|
|
|
|
STRUCT = "STRUCT"
|
|
|
|
kwds.append(STRUCT)
|
|
|
|
SWITCH = "SWITCH"
|
|
|
|
kwds.append(SWITCH)
|
|
|
|
TYPEDEF = "TYPEDEF"
|
|
|
|
kwds.append(TYPEDEF)
|
|
|
|
UNION = "UNION"
|
|
|
|
kwds.append(UNION)
|
|
|
|
UNSIGNED = "UNSIGNED"
|
|
|
|
kwds.append(UNSIGNED)
|
|
|
|
VOID = "VOID"
|
|
|
|
kwds.append(VOID)
|
|
|
|
VOLATILE = "VOLATILE"
|
|
|
|
kwds.append(VOLATILE)
|
|
|
|
WHILE = "WHILE"
|
|
|
|
kwds.append(WHILE)
|
2023-11-07 05:42:39 -04:00
|
|
|
# An instruction in the DSL
|
|
|
|
INST = "INST"
|
|
|
|
kwds.append(INST)
|
|
|
|
# A micro-op in the DSL
|
|
|
|
OP = "OP"
|
|
|
|
kwds.append(OP)
|
|
|
|
# A macro in the DSL
|
|
|
|
MACRO = "MACRO"
|
|
|
|
kwds.append(MACRO)
|
2023-08-18 10:42:45 -03:00
|
|
|
keywords = {name.lower(): name for name in kwds}
|
|
|
|
|
2023-11-07 05:42:39 -04:00
|
|
|
ANNOTATION = "ANNOTATION"
|
2024-01-12 13:30:27 -04:00
|
|
|
annotations = {
|
|
|
|
"specializing",
|
|
|
|
"override",
|
|
|
|
"register",
|
|
|
|
"replaced",
|
|
|
|
"pure",
|
2024-02-20 06:50:59 -04:00
|
|
|
"split",
|
|
|
|
"replicate",
|
2024-02-23 13:31:57 -04:00
|
|
|
"tier1",
|
|
|
|
"tier2",
|
2024-01-12 13:30:27 -04:00
|
|
|
}
|
2023-11-07 05:42:39 -04:00
|
|
|
|
2023-08-18 10:42:45 -03:00
|
|
|
__all__ = []
|
|
|
|
__all__.extend(kwds)
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
def make_syntax_error(
|
2023-08-18 10:42:45 -03:00
|
|
|
message: str,
|
|
|
|
filename: str | None,
|
|
|
|
line: int,
|
|
|
|
column: int,
|
|
|
|
line_text: str,
|
2022-11-03 01:31:26 -03:00
|
|
|
) -> SyntaxError:
|
|
|
|
return SyntaxError(message, (filename, line, column, line_text))
|
|
|
|
|
|
|
|
|
2024-08-07 14:23:53 -03:00
|
|
|
@dataclass(slots=True, frozen=True)
|
2022-11-03 01:31:26 -03:00
|
|
|
class Token:
|
2023-12-07 08:49:40 -04:00
|
|
|
filename: str
|
2022-11-03 01:31:26 -03:00
|
|
|
kind: str
|
|
|
|
text: str
|
|
|
|
begin: tuple[int, int]
|
|
|
|
end: tuple[int, int]
|
|
|
|
|
|
|
|
@property
|
2023-08-18 10:42:45 -03:00
|
|
|
def line(self) -> int:
|
2022-11-03 01:31:26 -03:00
|
|
|
return self.begin[0]
|
|
|
|
|
|
|
|
@property
|
2023-08-18 10:42:45 -03:00
|
|
|
def column(self) -> int:
|
2022-11-03 01:31:26 -03:00
|
|
|
return self.begin[1]
|
|
|
|
|
|
|
|
@property
|
2023-08-18 10:42:45 -03:00
|
|
|
def end_line(self) -> int:
|
2022-11-03 01:31:26 -03:00
|
|
|
return self.end[0]
|
|
|
|
|
|
|
|
@property
|
2023-08-18 10:42:45 -03:00
|
|
|
def end_column(self) -> int:
|
2022-11-03 01:31:26 -03:00
|
|
|
return self.end[1]
|
|
|
|
|
|
|
|
@property
|
2023-08-18 10:42:45 -03:00
|
|
|
def width(self) -> int:
|
2022-11-03 01:31:26 -03:00
|
|
|
return self.end[1] - self.begin[1]
|
|
|
|
|
2023-08-18 10:42:45 -03:00
|
|
|
def replaceText(self, txt: str) -> "Token":
|
2022-11-03 01:31:26 -03:00
|
|
|
assert isinstance(txt, str)
|
2023-12-07 08:49:40 -04:00
|
|
|
return Token(self.filename, self.kind, txt, self.begin, self.end)
|
2022-11-03 01:31:26 -03:00
|
|
|
|
2023-08-18 10:42:45 -03:00
|
|
|
def __repr__(self) -> str:
|
2022-11-03 01:31:26 -03:00
|
|
|
b0, b1 = self.begin
|
|
|
|
e0, e1 = self.end
|
|
|
|
if b0 == e0:
|
|
|
|
return f"{self.kind}({self.text!r}, {b0}:{b1}:{e1})"
|
|
|
|
else:
|
|
|
|
return f"{self.kind}({self.text!r}, {b0}:{b1}, {e0}:{e1})"
|
|
|
|
|
|
|
|
|
2023-12-07 08:49:40 -04:00
|
|
|
def tokenize(src: str, line: int = 1, filename: str = "") -> Iterator[Token]:
|
2022-11-03 01:31:26 -03:00
|
|
|
linestart = -1
|
|
|
|
for m in matcher.finditer(src):
|
|
|
|
start, end = m.span()
|
|
|
|
text = m.group(0)
|
|
|
|
if text in keywords:
|
|
|
|
kind = keywords[text]
|
2023-11-07 05:42:39 -04:00
|
|
|
elif text in annotations:
|
|
|
|
kind = ANNOTATION
|
2022-11-03 01:31:26 -03:00
|
|
|
elif letter.match(text):
|
|
|
|
kind = IDENTIFIER
|
2023-08-18 10:42:45 -03:00
|
|
|
elif text == "...":
|
2022-11-03 01:31:26 -03:00
|
|
|
kind = ELLIPSIS
|
2023-08-18 10:42:45 -03:00
|
|
|
elif text == ".":
|
2022-11-03 01:31:26 -03:00
|
|
|
kind = PERIOD
|
2023-08-18 10:42:45 -03:00
|
|
|
elif text[0] in "0123456789.":
|
2022-11-03 01:31:26 -03:00
|
|
|
kind = NUMBER
|
|
|
|
elif text[0] == '"':
|
|
|
|
kind = STRING
|
|
|
|
elif text in opmap:
|
|
|
|
kind = opmap[text]
|
2023-08-18 10:42:45 -03:00
|
|
|
elif text == "\n":
|
2022-11-03 01:31:26 -03:00
|
|
|
linestart = start
|
|
|
|
line += 1
|
2023-08-18 10:42:45 -03:00
|
|
|
kind = "\n"
|
2022-11-03 01:31:26 -03:00
|
|
|
elif text[0] == "'":
|
|
|
|
kind = CHARACTER
|
2023-08-18 10:42:45 -03:00
|
|
|
elif text[0] == "#":
|
2023-11-07 05:42:39 -04:00
|
|
|
kind = CMACRO
|
2023-08-18 10:42:45 -03:00
|
|
|
elif text[0] == "/" and text[1] in "/*":
|
2022-11-03 01:31:26 -03:00
|
|
|
kind = COMMENT
|
|
|
|
else:
|
|
|
|
lineend = src.find("\n", start)
|
|
|
|
if lineend == -1:
|
|
|
|
lineend = len(src)
|
2023-08-18 10:42:45 -03:00
|
|
|
raise make_syntax_error(
|
|
|
|
f"Bad token: {text}",
|
|
|
|
filename,
|
|
|
|
line,
|
|
|
|
start - linestart + 1,
|
|
|
|
src[linestart:lineend],
|
|
|
|
)
|
2022-11-03 01:31:26 -03:00
|
|
|
if kind == COMMENT:
|
2023-08-18 10:42:45 -03:00
|
|
|
begin = line, start - linestart
|
|
|
|
newlines = text.count("\n")
|
2022-11-03 01:31:26 -03:00
|
|
|
if newlines:
|
2023-08-18 10:42:45 -03:00
|
|
|
linestart = start + text.rfind("\n")
|
2022-11-03 01:31:26 -03:00
|
|
|
line += newlines
|
|
|
|
else:
|
2023-08-18 10:42:45 -03:00
|
|
|
begin = line, start - linestart
|
2024-10-07 10:56:39 -03:00
|
|
|
if kind == CMACRO:
|
|
|
|
linestart = end
|
|
|
|
line += 1
|
2022-11-03 01:31:26 -03:00
|
|
|
if kind != "\n":
|
2024-01-12 13:30:27 -04:00
|
|
|
yield Token(
|
|
|
|
filename, kind, text, begin, (line, start - linestart + len(text))
|
|
|
|
)
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
def to_text(tkns: list[Token], dedent: int = 0) -> str:
|
|
|
|
res: list[str] = []
|
2023-08-18 10:42:45 -03:00
|
|
|
line, col = -1, 1 + dedent
|
2022-11-03 01:31:26 -03:00
|
|
|
for tkn in tkns:
|
|
|
|
if line == -1:
|
|
|
|
line, _ = tkn.begin
|
|
|
|
l, c = tkn.begin
|
2023-08-18 10:42:45 -03:00
|
|
|
# assert(l >= line), (line, txt, start, end)
|
2022-11-03 01:31:26 -03:00
|
|
|
while l > line:
|
|
|
|
line += 1
|
2023-08-18 10:42:45 -03:00
|
|
|
res.append("\n")
|
|
|
|
col = 1 + dedent
|
|
|
|
res.append(" " * (c - col))
|
2022-12-02 23:57:30 -04:00
|
|
|
text = tkn.text
|
2023-08-18 10:42:45 -03:00
|
|
|
if dedent != 0 and tkn.kind == "COMMENT" and "\n" in text:
|
2022-12-02 23:57:30 -04:00
|
|
|
if dedent < 0:
|
2023-08-18 10:42:45 -03:00
|
|
|
text = text.replace("\n", "\n" + " " * -dedent)
|
2022-12-02 23:57:30 -04:00
|
|
|
# TODO: dedent > 0
|
|
|
|
res.append(text)
|
2022-11-03 01:31:26 -03:00
|
|
|
line, col = tkn.end
|
2023-08-18 10:42:45 -03:00
|
|
|
return "".join(res)
|
2022-11-03 01:31:26 -03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
2023-08-18 10:42:45 -03:00
|
|
|
|
2022-11-03 01:31:26 -03:00
|
|
|
filename = sys.argv[1]
|
|
|
|
if filename == "-c":
|
|
|
|
src = sys.argv[2]
|
|
|
|
else:
|
|
|
|
src = open(filename).read()
|
|
|
|
# print(to_text(tokenize(src)))
|
|
|
|
for tkn in tokenize(src, filename=filename):
|
|
|
|
print(tkn)
|