@@ -153,12 +153,19 @@ def count_parse_action(tks: ParseResults) -> None:
153153
154154
155155def _dict_of (
156- keyword : ParserElement , data : ParserElement , * , located : bool = False
156+ keyword : ParserElement ,
157+ data : ParserElement ,
158+ * ,
159+ directive : ParserElement | None = None ,
160+ located : bool = False ,
157161) -> ParserElement :
158162 dict_ = Forward ()
159163
160164 keyword_entry = keyword + (dict_ | (data + Literal (";" ).suppress ()))
161165
166+ if directive is not None :
167+ keyword_entry |= directive + data + LineEnd ().suppress () # type: ignore [no-untyped-call]
168+
162169 if located :
163170 keyword_entry = Located (keyword_entry )
164171
@@ -175,12 +182,17 @@ def _keyword_entry_of(
175182 keyword : ParserElement ,
176183 data : ParserElement ,
177184 * ,
185+ directive : ParserElement | None = None ,
178186 located : bool = False ,
179187) -> ParserElement :
180188 keyword_entry = keyword + (
181- _dict_of (keyword , data , located = located ) | (data + Literal (";" ).suppress ())
189+ _dict_of (keyword , data , directive = directive , located = located )
190+ | (data + Literal (";" ).suppress ())
182191 )
183192
193+ if directive is not None :
194+ keyword_entry |= directive + data + LineEnd ().suppress () # type: ignore [no-untyped-call]
195+
184196 if located :
185197 keyword_entry = Located (keyword_entry )
186198 else :
@@ -240,7 +252,8 @@ def _keyword_entry_of(
240252 | _tensor_list (TensorKind .TENSOR , ignore = _COMMENT )
241253 )
242254)
243- _TOKEN = dbl_quoted_string | _IDENTIFIER
255+ _DIRECTIVE = Word ("#" , _IDENTBODYCHARS )
256+ _TOKEN = dbl_quoted_string | _IDENTIFIER | _DIRECTIVE
244257_DATA = Forward ()
245258_KEYWORD_ENTRY = _keyword_entry_of (_TOKEN | _list_of (_IDENTIFIER ), _DATA )
246259_DICT = _dict_of (_TOKEN , _DATA )
@@ -259,18 +272,21 @@ def _keyword_entry_of(
259272
260273
261274def parse_data (s : str ) -> Data :
275+ if not s .strip ():
276+ return ""
262277 return cast (Data , _DATA .parse_string (s , parse_all = True )[0 ])
263278
264279
265280_LOCATED_DICTIONARY = Group (
266- _keyword_entry_of (_TOKEN , Opt (_DATA , default = "" ), located = True )
281+ _keyword_entry_of (
282+ _TOKEN , Opt (_DATA , default = "" ), directive = _DIRECTIVE , located = True
283+ )
267284)[...]
268285_LOCATED_DATA = Group (Located (_DATA .copy ().add_parse_action (lambda tks : ["" , tks [0 ]])))
269286
270287_FILE = (
271288 Dict (_LOCATED_DICTIONARY + Opt (_LOCATED_DATA ) + _LOCATED_DICTIONARY )
272289 .ignore (_COMMENT )
273- .ignore (Literal ("#include" ) + ... + LineEnd ()) # type: ignore [no-untyped-call]
274290 .parse_with_tabs ()
275291)
276292
0 commit comments