Skip to content

Base parser

base_parser

Code to support parsing EDK2 files.

BaseParser

Base Parser for other parser objects.

Attributes:

Name Type Description
Parsed bool

If a file has been parsed or not

Lines list

order list of lines in the file

LocalVars dict

Dict of local variables

InputVars dict

Dict of Input variables

ConditionalStack list

list of current condition expressions

RootPath str

Workspace root

PPs list

List of PPs

TargetFilePath list

file being parsed

__init__

__init__(log: str = 'BaseParser') -> BaseParser

Inits an empty Parser.

SetEdk2Path

SetEdk2Path(pathobj: path_utilities.Edk2Path) -> BaseParser

Sets the internal attribute Edk2PathUtil.

Note

This is a drop in replacement for SetBaseAbsPath and SetPackagePaths as it will asssign both RootPath and PPs using the Edk2Path object attributes WorkspacePath and PackagePathList respectively.

SetBaseAbsPath/SetPackagePaths integration instructions:

# Previous Way
parser = BaseParser()
parser.SetBaseAbsPath(path)
parser.SetPackagePaths(pps)
# Integration
parser = BaseParser()
parser.SetEdk2Path(Edk2Path(path, pps))

```python

Integrate with no pps

parser = BaseParser() parser.SetEdk2Path(Edk2Path(path, []))

Args: pathobj (Edk2Path): Edk2Path object

Returns: (BaseParser): self

SetBaseAbsPath

SetBaseAbsPath(path: str) -> BaseParser

Sets the attribute RootPath.

Parameters:

Name Type Description Default
path str

Abs root path

required

Returns:

Type Description
BaseParser

self

SetPackagePaths

SetPackagePaths(
    pps: Optional[list[str]] = None,
) -> BaseParser

Sets the attribute PPs.

Parameters:

Name Type Description Default
pps
None

Returns:

Type Description
BaseParser

self

SetInputVars

SetInputVars(inputdict: dict) -> BaseParser

Sets the attribute InputVars.

Parameters:

Name Type Description Default
inputdict dict

The input vars dictionary

required

Returns:

Type Description
BaseParser

self

FindPath

FindPath(*p: str) -> str

Given a path, it will find it relative to the root, the current target file, or the packages path.

Parameters:

Name Type Description Default
*p obj

any number of strings or path like objects

()

Returns:

Type Description
str

a full absolute path if the file exists

None

None on failure

WriteLinesToFile

WriteLinesToFile(filepath: str) -> None

Write all parsed lines to a file.

Parameters:

Name Type Description Default
filepath str

path to an unopened file

required

ComputeResult

ComputeResult(
    value: Union[str, int],
    cond: str,
    value2: Union[str, int],
) -> bool

Compute a logical comaprison.

Parameters:

Name Type Description Default
value Union[str, int]

First value

required
cond str

comparison to do

required
value2 Union[str, int]

Second value

required

Returns:

Type Description
bool

result of comparison

ConvertToInt

ConvertToInt(value: Union[str, int]) -> int

Converts a str or int to an int based on prefix.

Parameters:

Name Type Description Default
value (str, int)

value to convert

required

Returns:

Type Description
int

Converted value

PushConditional

PushConditional(
    v: bool, already_true: bool = False
) -> None

Push new value onto the conditional stack.

Parameters:

Name Type Description Default
v bool

Value to push

required
already_true bool

A boolean that specifies if this condition (if/elseif/else) block has already been true.

False

Note

already_true is needed when calling PopConditional() to know if the next part of the conditional block needs evaluated or not.

PopConditional

PopConditional() -> bool

Pops the current conditional and return the value.

Additionally returns a value specifying if the if/elseif/else block has already returned true. This is needed to know if the next part of the conditional block needs evaluated or not.

Returns (bool, bool): (value, already_true)

ReplaceVariables

ReplaceVariables(line: str) -> str

Replaces a variable in a string.

Parameters:

Name Type Description Default
line str

The line to process

required

Returns:

Type Description
str

The line with the replaced variable.

ProcessConditional

ProcessConditional(text: str) -> bool

Processes a conditional.

Parameters:

Name Type Description Default
text str

The text to process

required

Returns:

Type Description
bool

true if a line is a conditiona otherwise false

EvaluateConditional

EvaluateConditional(text: str) -> bool

Uses a pushdown resolver.

InActiveCode

InActiveCode() -> bool

Determines what the state of the conditional you are currently in.

Returns:

Type Description
bool

result of the state of the conditional you are in.

IsGuidString

IsGuidString(line: str) -> str

Determines if a line is a guid string.

Parameters:

Name Type Description Default
line str

line representing a possible guid string

required

Returns:

Type Description
bool

whether the string is a guid string

Will return true if the the line has

ParseGuid

ParseGuid(line: str) -> str

Parse a guid into a different format.

Parameters:

Name Type Description Default
line str

the guid to parse ex: { 0xD3B36F2C, 0xD551, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}

required

Returns:

Type Description
str

guid. ex: D3B36F2C-D551-11D4-9A46-0090273FC14D

Raises:

Type Description
RuntimeError

if missing any of the 11 parts, or it isn't long enough.

ResetParserState

ResetParserState() -> None

Resets the state of the parser.

HashFileParser

Base class for Edk2 build files that use # for comments.

__init__

__init__(log: str) -> HashFileParser

Inits an empty Parser for files that use # for comments..

StripComment

StripComment(line: str) -> str

Removes a comment from a line.

Parameters:

Name Type Description Default
line str

line with a comment (#)

required

ParseNewSection

ParseNewSection(line: str) -> tuple[bool, str]

Parses a new section line.

Parameters:

Name Type Description Default
line str

line representing a new section.

required