fcsql.parser

fcsql.parser.OCCURS_UNBOUNDED = -1

Atom occurrence if not bound.

class fcsql.parser.QueryNodeType(value)[source]

Node types of FCS-QL expression tree nodes.

QUERY_SEGMENT = 'QuerySegment'

Segment query.

QUERY_GROUP = 'QueryGroup'

Group query.

QUERY_SEQUENCE = 'QuerySequence'

Sequence query.

QUERY_DISJUNCTION = 'QueryDisjunction'

Or query.

QUERY_WITH_WITHIN = 'QueryWithWithin'

Query with within part.

EXPRESSION = 'Expression'

Simple expression.

EXPRESSION_WILDCARD = 'Wildcard'

Wildcard expression.

EXPRESSION_GROUP = 'Group'

Group expression.

EXPRESSION_OR = 'Or'

Or expression.

EXPRESSION_AND = 'And'

And expression.

EXPRESSION_NOT = 'Not'

Not expression.

SIMPLE_WITHIN = 'SimpleWithin'

Simple within part.

class fcsql.parser.Operator(value)[source]

FCS-QL operators.

EQUALS = 'Eq'

EQUALS operator.

NOT_EQUALS = 'Ne'

NOT-EQUALS operator.

class fcsql.parser.RegexFlag(value)[source]

FCS-QL expression tree regex flags.

char: str
CASE_INSENSITIVE = 'case-insensitive'

Case insensitive.

CASE_SENSITIVE = 'case-sensitive'

Case sensitive.

LITERAL_MATCHING = 'literal-matching'

match exactly (= literally)

IGNORE_DIACRITICS = 'ignore-diacritics'

Ignore all diacritics.

class fcsql.parser.SimpleWithinScope(value)[source]

The within scope.

SENTENCE = 'Sentence'

sentence scope (small)

UTTERANCE = 'Utterance'

utterance scope (small)

PARAGRAPH = 'Paragraph'

paragraph scope (medium)

TURN = 'Turn'

turn scope (medium)

TEXT = 'Text'

text scope (large)

SESSION = 'Session'

session scope (large)

class fcsql.parser.QueryVisitor[source]

Interface implementing a Visitor pattern for FCS-QL expression trees.

Default method implementations do nothing.

visit(node: QueryNode) None[source]

Visit a query node. Generic handler, dispatches to visit methods based on QueryNodeType if exists else do nothing:

method = "visit_" + node.node_type.value
Parameters:

node – the node to visit

Returns:

None

class fcsql.parser.QueryNode(node_type: QueryNodeType, children: List[QueryNode] | None = None, child: QueryNode | None = None)[source]

Base class for FCS-QL expression tree nodes.

[Constructor]

Parameters:
  • node_type – the type of the node

  • children – the children of this node or None. Defaults to None.

  • child – the child of this node or None. Defaults to None.

node_type

The node type of this node.

parent: QueryNode | None

The parent node of this node.

None if this is the root node.

children

The children of this node.

has_node_type(node_type: QueryNodeType) bool[source]

Check, if node if of given type.

Parameters:

node_type – type to check against

Returns:

boolTrue if node is of given type, False otherwise

Raises:

TypeError – if node_type is None

property child_count: int

Get the number of children of this node.

Returns:

int – the number of children of this node

get_child(idx: int, clazz: Type[_T] | None = None) QueryNode | None[source]

Get a child node of specified type by index.

When supplied with clazz parameter, only child nodes of the requested type are counted.

Parameters:
  • idx – the index of the child node (if clazz provided, only consideres child nodes of requested type)

  • clazz – the type to nodes to be considered, optional

Returns:

QueryNode – the child node of this node or None if not child was found (e.g. type mismatch or index out of bounds)

get_first_child(clazz: Type[_T] | None = None) QueryNode | None[source]

Get this first child node.

Parameters:

clazz – the type to nodes to be considered

Returns:

QueryNode – the first child node of this node or None

get_last_child(clazz: Type[_T] | None = None) QueryNode | None[source]

Get this last child node.

Parameters:

clazz – the type to nodes to be considered

Returns:

QueryNode – the last child node of this node or None

abstract accept(visitor: QueryVisitor) None[source]
class fcsql.parser.Expression(qualifier: str | None, identifier: str, operator: Operator, regex: str, regex_flags: Set[RegexFlag] | None)[source]

A FCS-QL expression tree SIMPLE expression node.

[Constructor]

Parameters:
  • qualifier – the layer identifier qualifier or None

  • identifier – the layer identifier

  • operator – the operator

  • regex – the regular expression

  • regex_flags – the regular expression flags or None

qualifier

The Layer Type Identifier qualifier.

None if not used in this expression.

identifier

The layer identifier.

operator

The operator.

regex

The regex value.

regex_flags

The regex flags set.

None if no flags were used in this expression.

has_layer_identifier(identifier: str) bool[source]

Check if the expression used a given Layer Type Identifier.

Parameters:

identifier – the Layer Type Identifier to check against

Returns:

boolTrue if this identifier was used, False otherwise

Raises:

TypeError – if identifier is None

is_layer_qualifier_empty() bool[source]

Check if the Layer Type Identifier qualifier is empty.

Returns:

boolTrue if no Layer Type Identifier qualifier was set, False otherwise

has_layer_qualifier(qualifier: str) bool[source]

Check if the expression used a given qualifier for the Layer Type Identifier.

Parameters:

qualifier – the qualifier to check against

Returns:

boolTrue if this identifier was used, False otherwise

Raises:

TypeError – if qualifier is None

has_operator(operator: Operator) bool[source]

Check if expression used a given operator.

Parameters:

operator – the operator to check

Returns:

boolTrue if the given operator was used, False otherwise

Raises:

TypeError – if operator is None

is_regex_flags_empty() bool[source]

Check if a regex flag set is empty.

Returns:

boolTrue if no regex flags where set, False otherwise

has_regex_flag(flag: RegexFlag) bool[source]

Check if a regex flag is set.

Parameters:

flag – the flag to be checked

Returns:

boolTrue if the flag is set, False otherwise

Raises:

TypeError – if flag is None

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.ExpressionWildcard(children: List[QueryNode] | None = None, child: QueryNode | None = None)[source]

A FCS-QL expression tree WILDCARD expression node.

[Constructor]

Parameters:
  • node_type – the type of the node

  • children – the children of this node or None. Defaults to None.

  • child – the child of this node or None. Defaults to None.

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.ExpressionGroup(child: QueryNode)[source]

A FCS-QL expression tree GROUP expression node.

[Constructor]

Parameters:

child – the group content

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.ExpressionNot(child: QueryNode)[source]

A FCS-QL expression tree NOT expression node.

[Constructor]

Parameters:

child – the child expression

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.ExpressionAnd(children: List[QueryNode])[source]

A FCS-QL expression tree AND expression node.

[Constructor]

Parameters:

children – child elements covered by AND expression.

property operands: List[QueryNode]

Get the AND expression operands.

Returns:

List[QueryNode] – a list of expressions

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.ExpressionOr(children: List[QueryNode])[source]

A FCS-QL expression tree OR expression node.

[Constructor]

Parameters:

children – child elements covered by OR expression.

property operands: List[QueryNode]

Get the OR expression operands.

Returns:

List[QueryNode] – a list of expressions

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.QueryDisjunction(children: List[QueryNode])[source]

A FCS-QL expression tree QR query.

[Constructor]

Parameters:

children – the children

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.QuerySequence(children: List[QueryNode])[source]

A FCS-QL expression tree query sequence node.

[Constructor]

Parameters:

children – the children for this node

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.QueryWithWithin(query: QueryNode, within: QueryNode | None)[source]

FCS-QL expression tree QUERY-WITH-WITHIN node.

[Constructor]

Parameters:
  • query – the query node

  • within – the within node

get_query() QueryNode[source]

Get the query clause.

Returns:

QueryNode – the query clause

get_within() QueryNode | None[source]

Get the within clause (= search context)

Returns:

QueryNode – the witin clause

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.QuerySegment(expression: QueryNode, min_occurs: int, max_occurs: int)[source]

A FCS-QL expression tree query segment node.

[Constructor]

Parameters:
  • expression – the expression

  • min_occurs – the minimum occurrence

  • max_occurs – the maximum occurrence

min_occurs

The minimum occurrence of this segment.

max_occurs

The maximum occurrence of this segment.

get_expression() QueryNode[source]

Get the expression for this segment.

Returns:

QueryNode – the expression

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.QueryGroup(child: QueryNode, min_occurs: int, max_occurs: int)[source]

A FCS-QL expression tree GROUP query node.

[Constructor]

Parameters:
  • child – the child

  • min_occurs – the minimum occurrence

  • max_occurs – the maximum occurrence

min_occurs

The minimum occurrence of group content.

max_occurs

The maximum occurrence of group content.

get_content() QueryNode[source]

Get the group content.

Returns:

QueryNode – the content of the GROUP query

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

class fcsql.parser.SimpleWithin(scope: SimpleWithinScope)[source]

A FCS-QL expression tree SIMPLE WITHIN query node.

[Constructor]

Parameters:
  • node_type – the type of the node

  • children – the children of this node or None. Defaults to None.

  • child – the child of this node or None. Defaults to None.

scope

The simple within scope.

accept(visitor: QueryVisitor) None[source]
parent: QueryNode | None

The parent node of this node.

None if this is the root node.

fcsql.parser.DEFAULT_UNICODE_NORMALIZATION_FORM = 'NFC'

Default unicode normalization form.

See also: unicodedata.normalize

class fcsql.parser.ErrorListener(query: str)[source]
syntaxError(recognizer, offendingSymbol, line, column, msg, e)[source]
has_errors() bool[source]
exception fcsql.parser.QueryParserException[source]

Query parser exception.

exception fcsql.parser.ExpressionTreeBuilderException[source]

Error building expression tree.

class fcsql.parser.ExpressionTreeBuilder(parser: QueryParser)[source]
stack_Query_disjunction: Deque[int]

for enterQuery_disjunction/exitQuery_disjunction

stack_Query_sequence: Deque[int]

for enterQuery_sequence/exitQuery_sequence

stack_Expression_or: Deque[int]

for enterExpression_or/exitExpression_or

stack_Expression_and: Deque[int]

for enterExpression_and/exitExpression_and

enterQuery(ctx: QueryContext)[source]
exitQuery(ctx: QueryContext)[source]
enterMain_query(ctx: Main_queryContext)[source]
exitMain_query(ctx: Main_queryContext)[source]
enterQuery_disjunction(ctx: Query_disjunctionContext)[source]
exitQuery_disjunction(ctx: Query_disjunctionContext)[source]
enterQuery_sequence(ctx: Query_sequenceContext)[source]
exitQuery_sequence(ctx: Query_sequenceContext)[source]
enterQuery_group(ctx: Query_groupContext)[source]
exitQuery_group(ctx: Query_groupContext)[source]
enterQuery_simple(ctx: Query_simpleContext)[source]
exitQuery_simple(ctx: Query_simpleContext)[source]
enterQuery_implicit(ctx: Query_implicitContext)[source]
exitQuery_implicit(ctx: Query_implicitContext)[source]
enterQuery_segment(ctx: Query_segmentContext)[source]
exitQuery_segment(ctx: Query_segmentContext)[source]
enterExpression_basic(ctx: Expression_basicContext)[source]
exitExpression_basic(ctx: Expression_basicContext)[source]
enterExpression_not(ctx: Expression_notContext)[source]
exitExpression_not(ctx: Expression_notContext)[source]
enterExpression_group(ctx: Expression_groupContext)[source]
exitExpression_group(ctx: Expression_groupContext)[source]
enterExpression_or(ctx: Expression_orContext)[source]
exitExpression_or(ctx: Expression_orContext)[source]
enterExpression_and(ctx: Expression_andContext)[source]
exitExpression_and(ctx: Expression_andContext)[source]
enterAttribute(ctx: AttributeContext)[source]
exitAttribute(ctx: AttributeContext)[source]
enterRegexp(ctx: RegexpContext)[source]
exitRegexp(ctx: RegexpContext)[source]
enterWithin_part_simple(ctx: Within_part_simpleContext)[source]
exitWithin_part_simple(ctx: Within_part_simpleContext)[source]
static processRepetition(ctx: QualifierContext) Tuple[int, int][source]
static processRepetitionRange(ctx: QuantifierContext) Tuple[int, int][source]
static getChildIndex(ctx: ParserRuleContext, start: int, ttype: int) int[source]
static parseInt(val: str) int[source]
static stripQuotes(val: str) str[source]
static unescapeString(val: str) str[source]
static unescapeUnicode(val: str, i: int, size: int) str[source]
static parseHexChar(val: str) int[source]
class fcsql.parser.QueryParser(default_identifier: str = 'text', default_operator: Operator = Operator.EQUALS, unicode_normalization_form: str | None = 'NFC')[source]

A FCS-QL query parser that produces FCS-QL expression trees.

[Constructor]

Parameters:
  • default_identifier – the default identifier to be used for simple expressions. Defaults to DEFAULT_IDENTIFIER.

  • default_operator – the default operator. Defaults to DEFAULT_OPERATOR.

  • unicode_normalization_form – the Unicode normalization form to be used or None to not perform normlization. Defaults to DEFAULT_UNICODE_NORMALIZATION_FORM.

parse(query: str) QueryNode[source]

Parse query.

Parameters:

query – the raw FCS-QL query

Raises:

QueryParserException – if an error occurred

Returns:

QueryNode – a FCS-QL expression tree