@@ -37,6 +37,50 @@ def sorting_name(n: str) -> str:
3737 return f"0 { n } "
3838
3939
40+ def parse_arguments (
41+ text : bytes , arg_start : int , initial_closing : int
42+ ) -> tuple [list [bytes ], int ]:
43+ """Parse macro arguments.
44+
45+ Parse macro arguments from `text[arg_start + 1:]` until the first
46+ unpaired occurrence of `initial_closing`.
47+
48+ Args:
49+ text (bytes): the string to parse
50+ arg_start (int): the start position
51+ initial_closing (int): the ASCII code of the closing bracket
52+
53+ Returns:
54+ tuple[list[bytes], int]:
55+ - list of arguments
56+ - position within `text` of the character after closing delimiter
57+ """
58+ args = []
59+ closing = [initial_closing ] # Stack of expected close brackets
60+ next_index = arg_start + 1
61+ while next_index < len (text ):
62+ if text [next_index ] == closing [- 1 ]:
63+ closing .pop ()
64+ if len (closing ) == 0 :
65+ args .append (text [arg_start + 1 : next_index ])
66+ break
67+ elif text [next_index ] in {ord (b"(" ), ord (b"{" )}:
68+ closing .append (
69+ ord (b")" ) if text [next_index ] == ord (b"(" ) else ord (b"}" )
70+ )
71+ elif (
72+ len (closing ) == 1
73+ and text [next_index ] == ord (b"," )
74+ and text [next_index - 1 ] != ord (b"\\ " )
75+ ):
76+ args .append (text [arg_start + 1 : next_index ])
77+ arg_start = next_index
78+ next_index += 1
79+ if next_index == len (text ):
80+ raise ValueError (f"missing { chr (closing [- 1 ])} " )
81+ return args , next_index + 1
82+
83+
4084def expand (
4185 inputs : list [Path ], output_path : Path , build_path : Optional [Path ] = Path ()
4286) -> None :
@@ -94,49 +138,6 @@ def find_object(obj: Path) -> Optional[Union[Path, list[os.DirEntry[str]]]]:
94138 dirents [obj / dirent .name ] = dirent
95139 return sorted (list (dirents .values ()), key = lambda x : sorting_name (x .name ))
96140
97- def parse_arguments (
98- text : bytes , arg_start : int , initial_closing : int
99- ) -> tuple [list [bytes ], int ]:
100- """Parse macro arguments.
101-
102- Parse macro arguments from `text[arg_start + 1:]` until the first
103- unpaired occurrence of `initial_closing`.
104-
105- Args:
106- text (bytes): the string to parse
107- arg_start (int): the start position
108- initial_closing (int): the ASCII code of the closing bracket
109-
110- Returns:
111- tuple[list[bytes], int]:
112- - list of arguments
113- - position within `text` of the character after closing delimiter
114- """
115- args = []
116- closing = [initial_closing ] # Stack of expected close brackets
117- next_index = arg_start + 1
118- while next_index < len (text ):
119- if text [next_index ] == closing [- 1 ]:
120- closing .pop ()
121- if len (closing ) == 0 :
122- args .append (text [arg_start + 1 : next_index ])
123- break
124- elif text [next_index ] in {ord (b"(" ), ord (b"{" )}:
125- closing .append (
126- ord (b")" ) if text [next_index ] == ord (b"(" ) else ord (b"}" )
127- )
128- elif (
129- len (closing ) == 1
130- and text [next_index ] == ord (b"," )
131- and text [next_index - 1 ] != ord (b"\\ " )
132- ):
133- args .append (text [arg_start + 1 : next_index ])
134- arg_start = next_index
135- next_index += 1
136- if next_index == len (text ):
137- raise ValueError (f"missing { chr (closing [- 1 ])} " )
138- return args , next_index + 1
139-
140141 def expand_bytes (
141142 text : bytes ,
142143 base_file : Path ,
0 commit comments