Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
Expand file tree
/
Copy pathparser.py
More file actions
Latest commit
76 lines (65 loc) · 1.91 KB
/
Copy pathparser.py
File metadata and controls
76 lines (65 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
fromparsingimport ( # noqa: F401
InstDef,
Macro,
Pseudo,
Family,
LabelDef,
Parser,
Context,
CacheEffect,
StackEffect,
InputEffect,
OpName,
AstNode,
Stmt,
SimpleStmt,
IfStmt,
ForStmt,
WhileStmt,
BlockStmt,
MacroIfStmt,
)
CodeDef=InstDef|LabelDef
defprettify_filename(filename: str) ->str:
# Make filename more user-friendly and less platform-specific,
# it is only used for error reporting at this point.
filename=filename.replace("\\", "/")
iffilename.startswith("./"):
filename=filename[2:]
iffilename.endswith(".new"):
filename=filename[:-4]
returnfilename
BEGIN_MARKER="// BEGIN BYTECODES //"
END_MARKER="// END BYTECODES //"
defparse_files(filenames: list[str]) ->list[AstNode]:
result: list[AstNode] = []
forfilenameinfilenames:
withopen(filename) asfile:
src=file.read()
psr=Parser(src, filename=prettify_filename(filename))
# Skip until begin marker
whiletkn:=psr.next(raw=True):
iftkn.text==BEGIN_MARKER:
break
else:
raisepsr.make_syntax_error(
f"Couldn't find {BEGIN_MARKER!r} in {psr.filename}"
)
start=psr.getpos()
# Find end marker, then delete everything after it
whiletkn:=psr.next(raw=True):
iftkn.text==END_MARKER:
break
delpsr.tokens[psr.getpos() -1 :]
# Parse from start
psr.setpos(start)
thing_first_token=psr.peek()
whilenode:=psr.definition():
assertnodeisnotNone
result.append(node) # type: ignore[arg-type]
ifnotpsr.eof():
psr.backup()
raisepsr.make_syntax_error(
f"Extra stuff at the end of {filename}", psr.next(True)
)
returnresult