The parser submodule

Warning

The functions and methods listed in this section are implementation details of CondConfigParser. Do not use them in your programs if you rely on API stability, as they are likely to change in incompatible ways without prior notice.

Parser module of CondConfigParser.

This module defines a Node class, one subclass for every node type that can occur in the abstract syntax tree, and a Parser class which implements the configuration file parsing using the output of the lexer (condconfigparser.lexer.Lexer).

class condconfigparser.parser.Node(children=None)[source]

Bases: object

Node class for the abstract syntax tree.

This class defines all the usual list methods; they act on the list referenced to by the children attribute.

Data belonging to the node that is not in the form of a Node instance (or instance of a subclass) must be stored in other attributes than children.

This class implements object.__eq__() and object.__ne__() for equality testing. This allows one to compare two abstract syntax trees by using == or != with the two root nodes.

children

List of children nodes (Node instances)

classmethod simplify(node, parent, childNum)[source]

Recursively replace useless AndTestNode and OrTestNode instances.

Every AndTestNode or OrTestNode that only has one child is replaced with this only child. This process is recursively done on all children of node.

Return the possibly-new root node of the tree.

undefVariables(varNames)[source]

Return the set of undefined variables for the tree rooted at self.

This method recursively explores the subtree rooted at self. If any node encountered refers to a variable whose name is not in varNames, it remembers that variable as undefined.

The return value is a frozenset containing a condconfigparser.lexer.VariableToken instance for every instance of an undefined variable in the tree rooted at self.

class condconfigparser.parser.RootNode(assignments, config)[source]

Bases: Node

Node representing the root of the abstract syntax tree for a config file/stream.

Corresponds to the root grammar production.

property assignments

VarAssignmentsNode instance.

property config

ConfigNode instance.

class condconfigparser.parser.VarAssignmentsNode(children=None)[source]

Bases: Node

Node representing zero or more variable assignments.

Corresponds to the varAssignments grammar production.

class condconfigparser.parser.AssignmentNode(lhs, rhs)[source]

Bases: Node

Node representing a variable assignment.

Corresponds to the variableAssignment grammar production.

property lhs

VariableNode instance for the assigned-to variable.

property name

Name of the assigned-to variable.

property rhs

Node instance representing the expression that defines the variable.

class condconfigparser.parser.ConfigNode(defaultConfigLines=None, sections=None)[source]

Bases: Node

Node representing the default, unconditional section of the config file as well as the list of its conditional sections.

Corresponds to the config grammar production.

defaultConfigLines

List of unconditional configuration lines (str instances)

property sections

List of SectionNode instances.

class condconfigparser.parser.SectionNode(startToken, predicate, rawConfigLines)[source]

Bases: Node

Node representing a conditional section of the config file.

Corresponds to the section grammar production.

property predicate

Abstract syntax tree representing the predicate.

This may be an OrTestNode, an AndTestNode, a ListLiteralNode, etc. In any case, it is an instance of a Node subclass.

class condconfigparser.parser.OrTestNode(children=None)[source]

Bases: Node

Node representing a short-circuit, logical or test.

The semantics are the same as in Python, including the result of the evaluation. cf.

Corresponds to the orTest grammar production.

eval(context)[source]

Return the node value, after recursive evaluation of its children.

class condconfigparser.parser.AndTestNode(children=None)[source]

Bases: Node

Node representing a short-circuit, logical and test.

The semantics are the same as in Python, including the result of the evaluation. cf.

Corresponds to the andTest grammar production.

eval(context)[source]

Return the node value, after recursive evaluation of its children.

class condconfigparser.parser.NotTestNode(child)[source]

Bases: Node

Node representing a logical not test.

Related to the notTest grammar production.

eval(context)[source]

Return the node value, after recursive evaluation of its child.

property child

Child node of the NotTestNode instance.

It is an instance of a Node subclass.

class condconfigparser.parser.BinOpNodeBase(opToken, lOp, rOp)[source]

Bases: Node

Base class for nodes having lOp and rOp properties.

property lOp

Left operand of the binary operator.

It is an instance of a subclass of Node.

property rOp

Right operand of the binary operator.

It is an instance of a subclass of Node.

class condconfigparser.parser.EqualsTestNode(opToken, lOp, rOp)[source]

Bases: BinOpNodeBase

Node representing an == test.

Corresponds to the equalsTest grammar production.

eval(context)[source]

Return the node value, after recursive evaluation of its children.

class condconfigparser.parser.NotEqualsTestNode(opToken, lOp, rOp)[source]

Bases: BinOpNodeBase

Node representing an != test.

Corresponds to the notEqualsTest grammar production.

eval(context)[source]

Return the node value, after recursive evaluation of its children.

class condconfigparser.parser.InTestNode(opToken, lOp, rOp)[source]

Bases: BinOpNodeBase

Node representing an in test.

Corresponds to the inTest grammar production.

eval(context)[source]

Return the node value, after recursive evaluation of its children.

class condconfigparser.parser.VariableNode(variableToken)[source]

Bases: Node

Node representing a variable reference.

Corresponds to the variable grammar symbol.

token

condconfigparser.lexer.VariableToken instance

name

Variable name (str)

eval(context)[source]

Return the node value, according to context.

The node value is the value of the variable whose name is given by name, according to context.

undefVariables(varNames)[source]

Return the set of undefined variables.

The set of undefined variables is determined under the assumption that every variable with a name in varNames is defined.

class condconfigparser.parser.StringLiteralNode(value)[source]

Bases: Node

Node representing a string literal.

Corresponds to the stringLiteral grammar symbol.

value

The actual string represented by the node (str instance)

eval(context)[source]

Return the node value, which is the string literal represented by the node.

class condconfigparser.parser.BoolLiteralNode(value)[source]

Bases: Node

Node representing a boolean literal (i.e., True or False).

Corresponds to the boolLiteral grammar symbol.

value

The actual boolean represented by the node (bool instance)

eval(context)[source]

Return the node value, which is the boolean literal represented by the node.

class condconfigparser.parser.ListLiteralNode(items)[source]

Bases: Node

Node representing a list literal.

The elements of the list are represented by the Node instances forming the list referred to by the children attribute (same order, of course).

Corresponds to the listLiteral grammar symbol.

eval(context)[source]

Return the node value, which is the list literal represented by the node.

class condconfigparser.parser.Parser(lexer)[source]

Bases: object

Parser class of CondConfigParser.

This class implements a recursive descent parser that performs look-ahead in order to avoid any need for backtracking. The algorithm is typical of a LL(1) parser that does not use parse tables.

For more information, you may refer to:

lexer

condconfigparser.lexer.Lexer instance

queue

Queue holding the tokens generated by the lexer

tokenGenerator

Token generator from lexer

lastToken

Last token popped from queue, i.e., consumed by the parser

enqueue(num)[source]

Pull num tokens from tokenGenerator, pushing them into queue.

peekAt(index)[source]

Look at an element of queue without consuming it.

If index is 0, look at the first unprocessed token in queue. If index is 1, look at the next unprocessed token, etc..

Return the looked-at token, or None if it does not exist.

peekSeveral(num)[source]

Return the topmost num tokens without consuming them.

Return a list of length at most num. If EOF is reached before enough tokens can be read, the returned list will have less than num elements.

If the returned list l has at least one element, l[0] is the first unprocessed token; if it has at least two elements, l[1] is the second unprocessed token, etc.

readToken()[source]

Read a token from queue.

If there are no tokens in queue, return None. Otherwise, consume one token from queue, save it in lastToken for later reference and return it.

match1(tokenType)[source]

Match one token of type tokenType.

Read a token from queue. If none is available or if the token is not of type tokenType, raise ParseError. Otherwise, return the token that was read.

matchZeroOrMore(tokenType)[source]

Match zero or more tokens of type tokenType.

Return the list of matched tokens (the match is greedy).

matchOneOrMore(tokenType)[source]

Match one or more tokens of type tokenType.

Return the list of matched tokens (the match is greedy). Raise ParseError if the first unprocessed token is not of type tokenType.

root()[source]

Match a root production.

varAssignments()[source]

Match a varAssignments production.

varAssigs()[source]

Match a varAssigs production.

variableAssignment()[source]

Match a variableAssignment production.

config()[source]

Match a config production.

section()[source]

Match a section production.

predicate()[source]

Match a predicate production.

orTest()[source]

Match an orTest production.

andTest()[source]

Match an andTest production.

notTest()[source]

Match a notTest production.

atomicBool()[source]

Match an atomicBool production.

equalsTest(lOp)[source]

Match an equalsTest production.

notEqualsTest(lOp)[source]

Match a notEqualsTest production.

inTest(lOp)[source]

Match an inTest production.

expr()[source]

Match an expr production.

literal()[source]

Match a literal production.

boolLiteral()[source]

Match a boolLiteral production.

listLiteral()[source]

Match a listLiteral production.

buildTree()[source]

Parse a complete configuration file.

Return the root node of the corresponding abstract syntax tree, after simplification (cf. Node.simplify()).