Skip to content

Commit 3d23375

Browse files
committed
Make content optional
- Some preprocessors read the file directly
1 parent d94df61 commit 3d23375

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

cxxheaderparser/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Callable, Optional
33

44
#: arguments are (filename, content)
5-
PreprocessorFunction = Callable[[str, str], str]
5+
PreprocessorFunction = Callable[[str, Optional[str]], str]
66

77

88
@dataclass

cxxheaderparser/parser.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ class CxxParser:
7474
def __init__(
7575
self,
7676
filename: str,
77-
content: str,
77+
content: typing.Optional[str],
7878
visitor: CxxVisitor,
7979
options: typing.Optional[ParserOptions] = None,
80+
encoding: typing.Optional[str] = None,
8081
) -> None:
8182
self.visitor = visitor
8283
self.filename = filename
@@ -85,6 +86,13 @@ def __init__(
8586
if options and options.preprocessor is not None:
8687
content = options.preprocessor(filename, content)
8788

89+
if content is None:
90+
if encoding is None:
91+
encoding = "utf-8-sig"
92+
93+
with open(filename, "r", encoding=encoding) as fp:
94+
content = fp.read()
95+
8896
self.lex: lexer.TokenStream = lexer.LexerTokenStream(filename, content)
8997

9098
global_ns = NamespaceDecl([], False)

cxxheaderparser/preprocessor.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def make_gcc_preprocessor(
7474
if not encoding:
7575
encoding = "utf-8"
7676

77-
def _preprocess_file(filename: str, content: str) -> str:
77+
def _preprocess_file(filename: str, content: typing.Optional[str]) -> str:
7878
cmd = gcc_args + ["-w", "-E", "-C"]
7979

8080
for p in include_paths:
@@ -86,6 +86,8 @@ def _preprocess_file(filename: str, content: str) -> str:
8686
if filename == "<str>":
8787
cmd.append("-")
8888
filename = "<stdin>"
89+
if content is None:
90+
raise PreprocessorError("no content specified for stdin")
8991
kwargs["input"] = content
9092
else:
9193
cmd.append(filename)
@@ -191,7 +193,7 @@ def make_pcpp_preprocessor(
191193
if pcpp is None:
192194
raise PreprocessorError("pcpp is not installed")
193195

194-
def _preprocess_file(filename: str, content: str) -> str:
196+
def _preprocess_file(filename: str, content: typing.Optional[str]) -> str:
195197
pp = _CustomPreprocessor(encoding, passthru_includes)
196198
if include_paths:
197199
for p in include_paths:
@@ -203,6 +205,10 @@ def _preprocess_file(filename: str, content: str) -> str:
203205
if not retain_all_content:
204206
pp.line_directive = "#line"
205207

208+
if content is None:
209+
with open(filename, "r", encoding=encoding) as fp:
210+
content = fp.read()
211+
206212
pp.parse(content, filename)
207213

208214
if pp.errors:

cxxheaderparser/simple.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ def parse_file(
348348
if filename == "-":
349349
content = sys.stdin.read()
350350
else:
351-
with open(filename, encoding=encoding) as fp:
352-
content = fp.read()
351+
content = None
353352

354-
return parse_string(content, filename=filename, options=options)
353+
visitor = SimpleCxxVisitor()
354+
parser = CxxParser(filename, content, visitor, options)
355+
parser.parse()
356+
357+
return visitor.data

0 commit comments

Comments
 (0)