Skip to content

Commit 675b07c

Browse files
committed
feat: Add support for new lineMarker types (numeric, 'REGEX', 'PREFIX', 'SUFFIX')
1 parent b4f2507 commit 675b07c

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
all: test version
44

55
version:
6+
git describe --tags
67
python -m setuptools_scm
78

89
test:

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ classifiers = [
2626
]
2727
keywords = ["parser", "ast", "cedarscript", "code-editing", "refactoring", "code-analysis", "sql-like", "ai-assisted-development"]
2828
dependencies = [
29-
"cedarscript-grammar>=0.0.16",
29+
"cedarscript-grammar==0.1.0",
3030
]
3131
requires-python = ">=3.8"
3232

src/cedarscript_ast_parser/cedarscript_ast_parser.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,34 @@ def as_marker(self) -> 'Marker':
4747
@dataclass
4848
class Marker(MarkerCompatible):
4949
"""
50-
See Marker.to_search_range
50+
A marker can be one of:
51+
- LINE with string/number value
52+
- LINE REGEX with regex pattern
53+
- LINE PREFIX with prefix string
54+
- LINE SUFFIX with suffix string
55+
- VARIABLE with name
56+
- FUNCTION with name
57+
- CLASS with name
58+
See also: Marker.to_search_range
5159
"""
5260
type: MarkerType
5361
value: str
5462
offset: int | None = None
63+
marker_subtype: str | None = None # 'REGEX', 'PREFIX', 'SUFFIX' for LINE type
5564

5665
@property
5766
def as_marker(self) -> 'Marker':
5867
return self
5968

6069
def __str__(self):
61-
result = f"{self.type.value} '{self.value.strip()}'"
70+
result = self.type.value
71+
match self.marker_subtype:
72+
case 'string' | None:
73+
pass
74+
case _:
75+
result += self.marker_subtype.value
76+
77+
result += f" '{self.value.strip()}'"
6278
if self.offset is not None:
6379
result += f" at offset {self.offset}"
6480
return result
@@ -533,13 +549,37 @@ def parse_region(self, node) -> Region:
533549
return result
534550

535551
def parse_marker(self, node) -> Marker:
536-
# TODO Fix: handle line marker as well
552+
# Handle marker inside marker_or_segment
537553
if node.type.casefold() == 'marker':
538554
node = node.named_children[0]
539-
marker_type = node.children[0].type # LINE, VARIABLE, FUNCTION, or CLASS
540-
value = self.parse_string(self.find_first_by_type(node.named_children, 'string'))
555+
556+
marker_type = node.children[0].type # LINE, VARIABLE, FUNCTION, METHOD or CLASS
557+
marker_subtype = None
558+
value = None
559+
560+
if marker_type != 'LINE': # VARIABLE, FUNCTION, METHOD or CLASS
561+
value = self.parse_string(self.find_first_by_type(node.named_children, 'string'))
562+
# Handle the different marker types
563+
else:
564+
# Get the second child which is either a string/number or a subtype specifier
565+
second_child = node.children[1]
566+
marker_subtype = second_child.type
567+
if second_child.type in ['string', 'number']:
568+
match second_child.type:
569+
case 'string':
570+
value = self.parse_string(second_child)
571+
case _:
572+
value = second_child.text.decode('utf8')
573+
else: # REGEX, PREFIX, or SUFFIX
574+
value = self.parse_string(node.children[2])
575+
541576
offset = self.parse_offset_clause(self.find_first_by_type(node.named_children, 'offset_clause'))
542-
return Marker(type=MarkerType(marker_type.casefold()), value=value, offset=offset)
577+
return Marker(
578+
type=MarkerType(marker_type.casefold()),
579+
marker_subtype=marker_subtype,
580+
value=value,
581+
offset=offset
582+
)
543583

544584
def parse_segment(self, node) -> Segment:
545585
relpos_start = self.find_first_by_type(node.named_children, 'relpos_segment_start').children[1]

0 commit comments

Comments
 (0)