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:
objectNode class for the abstract syntax tree.
This class defines all the usual
listmethods; they act on the list referenced to by thechildrenattribute.Data belonging to the node that is not in the form of a
Nodeinstance (or instance of a subclass) must be stored in other attributes thanchildren.This class implements
object.__eq__()andobject.__ne__()for equality testing. This allows one to compare two abstract syntax trees by using==or!=with the two root nodes.- classmethod simplify(node, parent, childNum)[source]¶
Recursively replace useless
AndTestNodeandOrTestNodeinstances.Every
AndTestNodeorOrTestNodethat 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
frozensetcontaining acondconfigparser.lexer.VariableTokeninstance for every instance of an undefined variable in the tree rooted at self.
- class condconfigparser.parser.RootNode(assignments, config)[source]¶
Bases:
NodeNode representing the root of the abstract syntax tree for a config file/stream.
Corresponds to the
rootgrammar production.- property assignments¶
VarAssignmentsNodeinstance.
- property config¶
ConfigNodeinstance.
- class condconfigparser.parser.VarAssignmentsNode(children=None)[source]¶
Bases:
NodeNode representing zero or more variable assignments.
Corresponds to the
varAssignmentsgrammar production.
- class condconfigparser.parser.AssignmentNode(lhs, rhs)[source]¶
Bases:
NodeNode representing a variable assignment.
Corresponds to the
variableAssignmentgrammar production.- property lhs¶
VariableNodeinstance for the assigned-to variable.
- property name¶
Name of the assigned-to variable.
- class condconfigparser.parser.ConfigNode(defaultConfigLines=None, sections=None)[source]¶
Bases:
NodeNode representing the default, unconditional section of the config file as well as the list of its conditional sections.
Corresponds to the
configgrammar production.- property sections¶
List of
SectionNodeinstances.
- class condconfigparser.parser.SectionNode(startToken, predicate, rawConfigLines)[source]¶
Bases:
NodeNode representing a conditional section of the config file.
Corresponds to the
sectiongrammar production.- property predicate¶
Abstract syntax tree representing the predicate.
This may be an
OrTestNode, anAndTestNode, aListLiteralNode, etc. In any case, it is an instance of aNodesubclass.
- class condconfigparser.parser.OrTestNode(children=None)[source]¶
Bases:
NodeNode representing a short-circuit, logical
ortest.The semantics are the same as in Python, including the result of the evaluation. cf.
Corresponds to the
orTestgrammar production.
- class condconfigparser.parser.AndTestNode(children=None)[source]¶
Bases:
NodeNode representing a short-circuit, logical
andtest.The semantics are the same as in Python, including the result of the evaluation. cf.
Corresponds to the
andTestgrammar production.
- class condconfigparser.parser.NotTestNode(child)[source]¶
Bases:
NodeNode representing a logical
nottest.Related to the
notTestgrammar production.- property child¶
Child node of the
NotTestNodeinstance.It is an instance of a
Nodesubclass.
- class condconfigparser.parser.BinOpNodeBase(opToken, lOp, rOp)[source]¶
Bases:
NodeBase class for nodes having
lOpandrOpproperties.
- class condconfigparser.parser.EqualsTestNode(opToken, lOp, rOp)[source]¶
Bases:
BinOpNodeBaseNode representing an
==test.Corresponds to the
equalsTestgrammar production.
- class condconfigparser.parser.NotEqualsTestNode(opToken, lOp, rOp)[source]¶
Bases:
BinOpNodeBaseNode representing an
!=test.Corresponds to the
notEqualsTestgrammar production.
- class condconfigparser.parser.InTestNode(opToken, lOp, rOp)[source]¶
Bases:
BinOpNodeBaseNode representing an
intest.Corresponds to the
inTestgrammar production.
- class condconfigparser.parser.VariableNode(variableToken)[source]¶
Bases:
NodeNode representing a variable reference.
Corresponds to the
variablegrammar symbol.- token¶
condconfigparser.lexer.VariableTokeninstance
- class condconfigparser.parser.StringLiteralNode(value)[source]¶
Bases:
NodeNode representing a string literal.
Corresponds to the
stringLiteralgrammar symbol.
- class condconfigparser.parser.BoolLiteralNode(value)[source]¶
Bases:
NodeNode representing a boolean literal (i.e.,
TrueorFalse).Corresponds to the
boolLiteralgrammar symbol.
- class condconfigparser.parser.ListLiteralNode(items)[source]¶
Bases:
NodeNode representing a list literal.
The elements of the list are represented by the
Nodeinstances forming the list referred to by thechildrenattribute (same order, of course).Corresponds to the
listLiteralgrammar symbol.
- class condconfigparser.parser.Parser(lexer)[source]¶
Bases:
objectParser 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.Lexerinstance
- enqueue(num)[source]¶
Pull num tokens from
tokenGenerator, pushing them intoqueue.
- peekAt(index)[source]¶
Look at an element of
queuewithout 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
Noneif 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, returnNone. Otherwise, consume one token fromqueue, save it inlastTokenfor 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, raiseParseError. 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
ParseErrorif the first unprocessed token is not of type tokenType.
- buildTree()[source]¶
Parse a complete configuration file.
Return the root node of the corresponding abstract syntax tree, after simplification (cf.
Node.simplify()).