Make content optional
- Some preprocessors read the file directly
This commit is contained in:
parent
d94df61c63
commit
3d23375190
@ -2,7 +2,7 @@ from dataclasses import dataclass
|
|||||||
from typing import Callable, Optional
|
from typing import Callable, Optional
|
||||||
|
|
||||||
#: arguments are (filename, content)
|
#: arguments are (filename, content)
|
||||||
PreprocessorFunction = Callable[[str, str], str]
|
PreprocessorFunction = Callable[[str, Optional[str]], str]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -74,9 +74,10 @@ class CxxParser:
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
filename: str,
|
filename: str,
|
||||||
content: str,
|
content: typing.Optional[str],
|
||||||
visitor: CxxVisitor,
|
visitor: CxxVisitor,
|
||||||
options: typing.Optional[ParserOptions] = None,
|
options: typing.Optional[ParserOptions] = None,
|
||||||
|
encoding: typing.Optional[str] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.visitor = visitor
|
self.visitor = visitor
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
@ -85,6 +86,13 @@ class CxxParser:
|
|||||||
if options and options.preprocessor is not None:
|
if options and options.preprocessor is not None:
|
||||||
content = options.preprocessor(filename, content)
|
content = options.preprocessor(filename, content)
|
||||||
|
|
||||||
|
if content is None:
|
||||||
|
if encoding is None:
|
||||||
|
encoding = "utf-8-sig"
|
||||||
|
|
||||||
|
with open(filename, "r", encoding=encoding) as fp:
|
||||||
|
content = fp.read()
|
||||||
|
|
||||||
self.lex: lexer.TokenStream = lexer.LexerTokenStream(filename, content)
|
self.lex: lexer.TokenStream = lexer.LexerTokenStream(filename, content)
|
||||||
|
|
||||||
global_ns = NamespaceDecl([], False)
|
global_ns = NamespaceDecl([], False)
|
||||||
|
@ -74,7 +74,7 @@ def make_gcc_preprocessor(
|
|||||||
if not encoding:
|
if not encoding:
|
||||||
encoding = "utf-8"
|
encoding = "utf-8"
|
||||||
|
|
||||||
def _preprocess_file(filename: str, content: str) -> str:
|
def _preprocess_file(filename: str, content: typing.Optional[str]) -> str:
|
||||||
cmd = gcc_args + ["-w", "-E", "-C"]
|
cmd = gcc_args + ["-w", "-E", "-C"]
|
||||||
|
|
||||||
for p in include_paths:
|
for p in include_paths:
|
||||||
@ -86,6 +86,8 @@ def make_gcc_preprocessor(
|
|||||||
if filename == "<str>":
|
if filename == "<str>":
|
||||||
cmd.append("-")
|
cmd.append("-")
|
||||||
filename = "<stdin>"
|
filename = "<stdin>"
|
||||||
|
if content is None:
|
||||||
|
raise PreprocessorError("no content specified for stdin")
|
||||||
kwargs["input"] = content
|
kwargs["input"] = content
|
||||||
else:
|
else:
|
||||||
cmd.append(filename)
|
cmd.append(filename)
|
||||||
@ -191,7 +193,7 @@ def make_pcpp_preprocessor(
|
|||||||
if pcpp is None:
|
if pcpp is None:
|
||||||
raise PreprocessorError("pcpp is not installed")
|
raise PreprocessorError("pcpp is not installed")
|
||||||
|
|
||||||
def _preprocess_file(filename: str, content: str) -> str:
|
def _preprocess_file(filename: str, content: typing.Optional[str]) -> str:
|
||||||
pp = _CustomPreprocessor(encoding, passthru_includes)
|
pp = _CustomPreprocessor(encoding, passthru_includes)
|
||||||
if include_paths:
|
if include_paths:
|
||||||
for p in include_paths:
|
for p in include_paths:
|
||||||
@ -203,6 +205,10 @@ def make_pcpp_preprocessor(
|
|||||||
if not retain_all_content:
|
if not retain_all_content:
|
||||||
pp.line_directive = "#line"
|
pp.line_directive = "#line"
|
||||||
|
|
||||||
|
if content is None:
|
||||||
|
with open(filename, "r", encoding=encoding) as fp:
|
||||||
|
content = fp.read()
|
||||||
|
|
||||||
pp.parse(content, filename)
|
pp.parse(content, filename)
|
||||||
|
|
||||||
if pp.errors:
|
if pp.errors:
|
||||||
|
@ -348,7 +348,10 @@ def parse_file(
|
|||||||
if filename == "-":
|
if filename == "-":
|
||||||
content = sys.stdin.read()
|
content = sys.stdin.read()
|
||||||
else:
|
else:
|
||||||
with open(filename, encoding=encoding) as fp:
|
content = None
|
||||||
content = fp.read()
|
|
||||||
|
|
||||||
return parse_string(content, filename=filename, options=options)
|
visitor = SimpleCxxVisitor()
|
||||||
|
parser = CxxParser(filename, content, visitor, options)
|
||||||
|
parser.parse()
|
||||||
|
|
||||||
|
return visitor.data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user