coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
expression_parser< CharSeq > Struct Template Reference

Encapsulates parsing of preprocessor expressions. More...

#include <expression_parser.h>

Public Types

enum  deletion_code { not_deleted, non_paren = 1, paren }
 Codes for logical deletion status. More...
 
enum  precedence {
  max = 10, ternary_either_or = 10, ternary_if = 10, comma = 9,
  boolean_or = 8, boolean_and = 7, bit_or = 6, bit_xor = 6,
  bit_and = 6, eq = 5, neq = 5, le = 4,
  ge = 4, lt = 4, gt = 4, lshift = 3,
  rshift = 3, add = 2, subtract = 2, mult = 1,
  divide = 1, mod = 1
}
 Codes for operator precedences.
 
using sequence_type = CharSeq
 Type of character-sequence containing the parsed expression.
 

Public Member Functions

 expression_parser (chewer< sequence_type > &chew, reference const *ref=nullptr)
 Construct given a chewer<sequence_type> More...
 
 expression_parser (sequence_type &seq, reference const *ref=nullptr, size_t start=0)
 Construct to evaluate a sequence_type More...
 
evaluation result () const
 Get the result of parsing the expression. More...
 
 operator evaluation () const
 Cast the object to an evaluation.
 
std::string simplified () const
 Get the most simplified form of the expression. More...
 
bool is_simplified () const
 Say whether the expression has been simplified.
 

Private Types

using infix_operation = evaluation(expression_parser::*)(evaluation const &, evaluation const &)
 Type of an infix operation.
 

Private Member Functions

evaluation op_lt (evaluation const &lhs, evaluation const &rhs)
 Less-than operator.
 
evaluation op_gt (evaluation const &lhs, evaluation const &rhs)
 Greater-than operator.
 
evaluation op_le (evaluation const &lhs, evaluation const &rhs)
 Less-than-or-equal operator.
 
evaluation op_ge (evaluation const &lhs, evaluation const &rhs)
 Greater-than-or-equal operator.
 
evaluation op_eq (evaluation const &lhs, evaluation const &rhs)
 Equality operator.
 
evaluation op_ne (evaluation const &lhs, evaluation const &rhs)
 Inequality operator.
 
evaluation op_or (evaluation const &lhs, evaluation const &rhs)
 Inclusive-or operator.
 
evaluation op_and (evaluation const &lhs, evaluation const &rhs)
 And operator.
 
evaluation op_ternary_if (evaluation const &lhs, evaluation const &rhs)
 The '?' "operator". Stacks its left operand; returns its right.
 
evaluation op_ternary_either_or (evaluation const &lhs, evaluation const &rhs)
 The ':' "operator". Pops the evaluation of the preceding `cond ?' from the stack. If cond is true, returns lhs; if cond is false returns rhs, else returns evaluation()
 
evaluation op_bit_and (evaluation const &lhs, evaluation const &rhs)
 Bitwise AND operator.
 
evaluation op_bit_or (evaluation const &lhs, evaluation const &rhs)
 Bitwise OR operator.
 
evaluation op_bit_xor (evaluation const &lhs, evaluation const &rhs)
 Bitwise XOR operator.
 
evaluation op_lshift (evaluation const &lhs, evaluation const &rhs)
 Left-shift operator.
 
evaluation op_rshift (evaluation const &lhs, evaluation const &rhs)
 Right-shift operator.
 
evaluation op_add (evaluation const &lhs, evaluation const &rhs)
 Addition operator.
 
evaluation op_subtract (evaluation const &lhs, evaluation const &rhs)
 Subtraction operator.
 
evaluation op_comma (evaluation const &lhs, evaluation const &rhs)
 Comma operator.
 
evaluation op_mult (evaluation const &lhs, evaluation const &rhs)
 Multiplication operator.
 
evaluation op_divide (evaluation const &lhs, evaluation const &rhs)
 Division operator.
 
evaluation op_mod (evaluation const &lhs, evaluation const &rhs)
 Modulus operator.
 
void parse (chewer< sequence_type > &chew)
 Evaluate text from a chewer<sequence_type> More...
 
evaluation unary_op (chewer< sequence_type > &chew, size_t end)
 Evaluator for innermost subexpressions. More...
 
template<unsigned Precedence>
evaluation infix_op (chewer< sequence_type > &chew, size_t end)
 Evaluator for infix operations. More...
 
evaluation apply (infix_operation op, evaluation &lhs, evaluation &rhs)
 Apply a infix operation to arguments. More...
 
template<unsigned Precedence>
std::pair
< expression_parser::infix_operation,
bool > 
seek_rightmost_infix (chewer< sequence_type > &chew, size_t end, size_t &op_start, size_t &op_end)
 Search for the rightmost binary operator at a given level of precedence within delimited text. More...
 
void delete_paren (size_t loff, size_t roff)
 Logically delete redundant parentheses.
 
void restore_paren (size_t loff, size_t roff)
 Restore a prematurely deleted pair of parentheses.
 
void restore_paren ()
 Restore all logically deleted parentheses.
 
void cut (size_t start, size_t end)
 Logically delete part of the evaluated text. More...
 
void defer_diagnostic (diagnostic_base &gripe)
 Defer a diagnostic, with the current line context appended if a source line is being parsed.
 
template<unsigned Precedence>
std::enable_if<(Precedence==0),
evaluation >::type 
next_evaluator (chewer< sequence_type > &chew, size_t end)
 Select the member function to recursively evaluate expressions in which the major operator has a given precedence.
 
template<unsigned Precedence>
std::enable_if<(Precedence >
0), evaluation >::type 
next_evaluator (chewer< sequence_type > &chew, size_t end)
 

Private Attributes

evaluation _eval
 The evaluation that results from parsing the expression.
 
std::vector< evaluation_ternary_cond_stack
 Stack of ternary conditional results;.
 
sequence_type_seq
 A reference to the character sequence being parsed.
 
std::vector< deletion_code_deletions
 Vector of logical deletion codes;.
 
size_t _start
 Text offset at which evaluation starts.
 
size_t _cuts
 Number of logically deleted operands.
 
ptrdiff_t _last_deletion
 Index of rightmost deletion, -1 if none.
 
reference const * _ref
 A pointer to the reference if any, that calls for expression parsing.
 

Detailed Description

template<class CharSeq>
struct expression_parser< CharSeq >

Encapsulates parsing of preprocessor expressions.

template struct expression_parser encapsulates the evaluation of all contexts that require resolution with respect to the defined and undefined symbols.

Definition at line 60 of file expression_parser.h.

Member Enumeration Documentation

template<class CharSeq>
enum expression_parser::deletion_code

Codes for logical deletion status.

Enumerator
not_deleted 

Not logically deleted.

non_paren 

A logically deleted non-parenthesis.

paren 

A logically deleted parenthesis.

Definition at line 67 of file expression_parser.h.

Constructor & Destructor Documentation

template<class CharSeq>
expression_parser< CharSeq >::expression_parser ( chewer< sequence_type > &  chew,
reference const *  ref = nullptr 
)
inline

Construct given a chewer<sequence_type>

Parameters
chewOn entry, a chewer<sequence_type> positioned to the offset in the associated sequence_type to scan. On return chew is positioned to the first offset not consumed.
refA pointer to the reference if any, that calls for expression parsing.

Definition at line 111 of file expression_parser.h.

template<class CharSeq>
expression_parser< CharSeq >::expression_parser ( sequence_type seq,
reference const *  ref = nullptr,
size_t  start = 0 
)

Construct to evaluate a sequence_type

Parameters
seqA sequence_type to be evaluated.
refA pointer to the reference if any, that calls for expression parsing.
startThe offset at which to start evaluation.

The constructed object will have parsed and seq offset `start.

Member Function Documentation

template<class CharSeq>
evaluation expression_parser< CharSeq >::apply ( infix_operation  op,
evaluation lhs,
evaluation rhs 
)
private

Apply a infix operation to arguments.

Parameters
opPointer to the operation to apply
lhsThe left-hand argument
rhsThe right-hand argument
Returns
The result of applying op to lhs and rhs
template<class CharSeq>
void expression_parser< CharSeq >::cut ( size_t  start,
size_t  end 
)
inlineprivate

Logically delete part of the evaluated text.

Parameters
startText offset at which to cut.
endText offset just past cut.

The chunk to be deleted is only logically deleted by this function. This avoids shuffling down terminal segments of the text in the course of evaluation, when the effect of such editing may be obliterated by subsequent deletions. Physical deletion is performed only once at the end of evaluation, by removing all logically deleted portions.

The member function is only called to delete a truth-functionally redundant operator and one of its operands.

Definition at line 412 of file expression_parser.h.

template<class CharSeq>
template<unsigned Precedence>
evaluation expression_parser< CharSeq >::infix_op ( chewer< sequence_type > &  chew,
size_t  end 
)
private

Evaluator for infix operations.

The function evaluates subexpressions that are infix operations.

The evaluator does shortcircuit evaluation for && and ||. It also simplifies binary subexpressions that can be solved only on one side.

Template Parameters
PrecedenceThe precedence of the operators that may be considered for evaluation.
Parameters
chewOn entry, a chewer<sequence_type> positioned to the offset in the associated sequence_type from which to scan. On return chew is positioned to the first offset not consumed.
endOffset beyond which not to evaluate
Returns
An evaluation representing the result of evaluation.
template<class CharSeq>
void expression_parser< CharSeq >::parse ( chewer< sequence_type > &  chew)
private

Evaluate text from a chewer<sequence_type>

Parameters
chewOn entry, a chewer<sequence_type> positioned to the offset in the associated sequence_type from which to scan. On return chew is positioned to the first offset not consumed.
template<class CharSeq>
evaluation expression_parser< CharSeq >::result ( ) const
inline

Get the result of parsing the expression.

Returns
The evaluation that results from parsing.

Definition at line 135 of file expression_parser.h.

template<class CharSeq>
template<unsigned Precedence>
std::pair<expression_parser::infix_operation,bool> expression_parser< CharSeq >::seek_rightmost_infix ( chewer< sequence_type > &  chew,
size_t  end,
size_t &  op_start,
size_t &  op_end 
)
private

Search for the rightmost binary operator at a given level of precedence within delimited text.

Template Parameters
PrecedenceThe precedence of the operators to be sought.
Parameters
chewA chewer<sequence_type> positioned to the offset in the associated sequence_type from which to search. On return chew is at the same position.
endOffset beyond which not to seach.
op_startInsignificant on entry. On return, receives the offset of the rightmost operator found, if any.
op_endInsignificant on entry. On return, receives the offset just past the rightmost operator found,if any.
Returns
A pair whose first element is pointer to the operation whose operator was found, if any, else nullptr, and whose second boolean element is true if the operation found is shortcuitable, else false.
template<class CharSeq>
std::string expression_parser< CharSeq >::simplified ( ) const

Get the most simplified form of the expression.

Returns
A string representing the expression simplified as much as possible by resolution of subexpressions.
template<class CharSeq>
evaluation expression_parser< CharSeq >::unary_op ( chewer< sequence_type > &  chew,
size_t  end 
)
private

Evaluator for innermost subexpressions.

Parameters
chewOn entry, a chewer<sequence_type> positioned to the offset in the associated sequence_type from which to scan. On return chew is positioned to the first offset not consumed.
endOffset beyond which not to evaluate.
Returns
An evaluation representing the result of evaluation.

The member function evaluates symbols, numbers and unary operations, including the parenthesis operation.


The documentation for this struct was generated from the following file: