Skip to content

Commit 3cb123a

Browse files
committed
feat: use optional cython compilation
1 parent 4f9741c commit 3cb123a

File tree

8 files changed

+170
-102
lines changed

8 files changed

+170
-102
lines changed

Taskfile.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ tasks:
77
generate-meta:
88
cmds:
99
- git checkout pegen/grammar_parser.py
10-
- python3 -m pegen pegen/metagrammar.gram -o pegen/grammar_parser.py
10+
- uv run python3 -m pegen pegen/metagrammar.gram -o pegen/grammar_parser.py
1111
- ruff format pegen/grammar_parser.py
1212
generate:
1313
deps:
1414
- dev
1515
cmds:
16-
- python3 tasks/generator.py
16+
- uv run python3 tasks/generator.py
1717
- ruff check --fix peg_parser/parser.py
1818
- ruff format peg_parser/parser.py
1919
- scc peg_parser/parser.py --uloc
@@ -32,8 +32,8 @@ tasks:
3232

3333
profile-tasks:
3434
cmds:
35-
- python tasks/profile_mem.py | tee -a {{.LOG_FILE}}
36-
- python tasks/simple.py | tee -a {{.LOG_FILE}}
35+
- uv run python tasks/profile_mem.py | tee -a {{.LOG_FILE}}
36+
- uv run python tasks/simple.py | tee -a {{.LOG_FILE}}
3737
vars:
3838
LOG_FILE:
3939
sh: echo ".local/logs/xonsh-parser-$(date "+%Y%m%d-%H%M%S").$(git rev-parse --short HEAD).log"
@@ -63,7 +63,7 @@ tasks:
6363
deps:
6464
- generate
6565
cmds:
66-
- python -m pytest {{.CLI_ARGS}}
66+
- uv run python -m pytest {{.CLI_ARGS}}
6767
# sources:
6868
# - '**/*.py'
6969

@@ -82,23 +82,23 @@ tasks:
8282

8383
memray:
8484
cmds:
85-
- pytest --memray
85+
- uv run pytest --memray
8686
mypy1:
8787
cmds:
88-
- mypy peg_parser
88+
- uv run mypy peg_parser
8989
mypy:
9090
cmds:
9191
- watchexec -e py,toml,gram --clear -- task generate mypy1
9292

9393
build_dep:
9494
cmds:
95-
- pip install setuptools_scm wheel
95+
- uv run uv pip install setuptools_scm wheel
9696

9797
mypycify:
9898
deps:
9999
- build_dep
100100
cmds:
101-
- env COMPILE_WITH_MYPYC=1 pip install -e . --no-build-isolation
101+
- env COMPILE_WITH_MYPYC=1 uv pip install -e . --no-build-isolation
102102
sources:
103103
- peg_parser/subheader.py
104104
- peg_parser/toke*.py
@@ -107,7 +107,7 @@ tasks:
107107
deps:
108108
- build_dep
109109
cmds:
110-
- env COMPILE_WITH_CYTHON=1 pip install -e . --no-build-isolation
110+
- env COMPILE_WITH_CYTHON=1 uv pip install -e . --no-build-isolation
111111
sources:
112112
- peg_parser/*.py
113113
- peg_parser/*.pxd
@@ -126,13 +126,13 @@ tasks:
126126

127127
pytest-bench:
128128
cmds:
129-
- pytest tests/benchmarks.py --benchmark-only --benchmark-autosave {{.CLI_ARGS}}
129+
- uv run pytest tests/benchmarks.py --benchmark-only --benchmark-autosave {{.CLI_ARGS}}
130130

131131
bench-lexer:
132132
deps:
133133
- dev
134134
cmds:
135-
- pytest tests/bench_lexer.py --benchmark-only --benchmark-autosave {{.CLI_ARGS}}
135+
- uv run pytest tests/bench_lexer.py --benchmark-only --benchmark-autosave {{.CLI_ARGS}}
136136

137137
pytest-bench-plot:
138138
desc: Generate an SVG histogram of all recorded benchmarks

peg_parser/parser.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from peg_parser.subheader cimport Parser
2+
3+
cdef class XonshParser(Parser):
4+
pass

peg_parser/subheader.pxd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import cython
3+
from peg_parser.tokenizer cimport Tokenizer
4+
5+
cdef class Parser:
6+
cdef public Tokenizer _tokenizer
7+
cdef public bint _verbose
8+
cdef public int _level
9+
cdef public list _caches
10+
cdef public object tok_cls
11+
cdef public int in_recursive_rule
12+
cdef public object _path_token
13+
cdef public int _index # index is int
14+
cdef public str filename
15+
cdef public object py_version
16+
cdef public bint call_invalid_rules
17+
18+
cpdef _mark(self)
19+
cpdef _reset(self, int index)

peg_parser/tokenize.pxd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
import cython
3+
4+
cdef class TokenInfo:
5+
cdef public tuple end
6+
cdef public tuple start
7+
cdef public str string
8+
cdef public object type
9+
10+
cpdef is_exact_type(self, str typ)
11+
cpdef loc_start(self)
12+
cpdef loc_end(self)
13+
cpdef loc(self)
14+
cpdef is_next_to(self, TokenInfo prev)

peg_parser/tokenizer.pxd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
import cython
3+
4+
cdef class Tokenizer:
5+
cdef object _readline
6+
cdef list _tokens
7+
cdef public int _index
8+
cdef bint _verbose
9+
cdef dict _lines
10+
cdef str _path
11+
cdef list _stack
12+
cdef bint _call_macro
13+
cdef bint _proc_macro
14+
cdef public list _raw_tokens
15+
cdef public int _raw_index
16+
cdef dict _end_parens
17+
cdef public object tok_cls
18+
cdef public object new_tok
19+
20+
cpdef getnext(self)
21+
cpdef peek(self)
22+
cpdef diagnose(self)
23+
cpdef get_last_non_whitespace_token(self)

peg_parser/tokenizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
SKIP_TOKENS = ("WS", "COMMENT", "NL")
1515

1616

17-
def rs_tokenize(source: str) -> list[TokenInfo]:
17+
def rs_tokenize(source: str) -> tuple:
1818
import winnow_parser as wp
1919

2020
from peg_parser.tokenize import TokenInfo
2121

2222
return wp.tokenize(source), wp.TokInfo, TokenInfo
2323

2424

25-
def py_tokenize(source: str) -> list[TokenInfo]:
25+
def py_tokenize(source: str) -> tuple:
2626
from peg_parser.tokenize import TokenInfo, generate_tokens
2727

2828
return list(generate_tokens(source)), TokenInfo, TokenInfo

tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ def _py_lex_input(inp: str) -> list[TokenInfo]:
5656
return tokens
5757

5858

59-
LEXERS = ["rust", "py"]
59+
LEXERS = [
60+
# "rust",
61+
"py"
62+
]
6063

6164

6265
@pytest.fixture(params=LEXERS, name="lexer")

0 commit comments

Comments
 (0)