Package eu.bandm.tools.ramus.ogma
This package provides facilities for constructing parsers from simple building blocks. Constructed parsers can be used immediately.
Parser Building Blocks
A Parser is a functional object that takes Input states to
Action search trees with Output solutions. Parsers act as
stateless functions and generally have no observable side-effects. Parsers
are reentrant, and can be reused both recursively and concurrently.
Parsers are parameterized with the type D of document identfiers
(used in source code locations) and with the type T of token types
(used in input tokens). It is recommended to use an enum type for
representing token types, but traditional encodings such as Integer
or String are also supported.
A parser output is a pair of a functional object that represents a semantic state transition, and the remaining (unconsumed) input state.
Input
A parser input is an iterator-like pointer into a stream of tokens
produced by a token source. Tokens and token sources are defined by Lexic.
If the input token source provides source code locations, these are recognized and passed through the parsing process for reference by abstract syntax tree (AST) objects.
Unlike iterators, input states are immutable. They store enough information about the token stream to make backtracking to previous positions possible for their whole lifetime.
Semantic States
A semantic State holds a single accumulator value and a
key–value map that serves as an environment of variables. The
key–value map is scoped; a pop() operation restores the mapping
in place before the corresponding push() operation. By contrast, the
accumulator is not scoped. The result of a semantic action is the
accumulator value in its final state.
The values held by both the accumulator and the map have a simple
domain-specific type system that is logically independent of Java types.
They are wrapped in instance of the interface Data.
Semantic states are immutable. Every modification operation creates an independent modified copy. This allows semantic states to be shared between nondeterministic branches without interference.
Parser Effects & Search Trees
Parser behavior, in addition to producing output states, is allowed to
fail and reject the input, to branch nondeterministically, and to emit
diagnostic messages. All of these are captured in a search-tree data
structure of type Action. The recommended way to process search
trees and harvest results is by means of a visitor.
Type Safety
Storing a value in a semantic state loses its static type information.
Safe access to semantic state values is supported by Data.View
projections, which have statically typed results and perform checked casts
under the hood.
A Reading is a matching pair of a parser and a type-correct
projection of result values. Readings are the recommended way to construct
complex parsers with type safety.
The relevant (context-free) grammar constructs are provided at the level of readings. For user-defined extensions, it is also recommended to define operations on readings rather than more low-level objects.
Semantic Actions
Map & friends.
Reduction.
Efficiency and Safety
In addition to their operational API, parsers provide metadata about their
behavior, which can be used for context-specific checks and optimizations. A
Predictor is an approximation of the token types consumed by a
parser. Predictor information is used transparently at construction time to
avoid nondeterministic branching by lookahead (of a single token) where
appropriate.
Parser construction operations accept Pragma
constants that control various ad-hoc aspects of disambiguation tactics and
optional sanity checks.
Configuration
Parsers and readings are subject to construction-time configuration. Hence they are created by factories.
Getting Started
- Configure a reading factory.
- Construct a reading that specifies the syntax and semantics.
- Connect it to a token source.
- Visit the search tree of interpretations.
-
ClassDescriptionAbstractExpression<D,
T, A> Base class for syntax–semantics pairs.Action<R,M, O> Base class of the parsing search-tree monad.Action.BiContinuation<R,S, T, M, O> Functional interface for binary continuations.Action.Continuation<R,S, M, O> Functional interface for search-tree continuations.Action.Success<R,M, O> Leaf node class of the parsing search-tree monad.Action.Visitor<R,M, O> Visitor base class for search trees.Interface of wrappers for semantic data.Data.View<A>Functional interface for unwrapping interpretation of semantic data.A placeholder for a view that can be initialized late.Interface of semantic environments.Base class of configurable factories for parsers and related objects.Interface of factory configuration settings.Fragment<D,T, A> A syntax–semantics pair that stores the result in a variable.Input<D,T> Interface of immutable parser input states.Output<D,T> Immutable parser transition outputs.Parser<D,T> Combinatorial nondeterministic parser building blocks.Enumeration of local pragmatic modifiers of parsing behavior.Configurable operations for constructing parsers.Interface for location tracking service.Predictor<T>Approximation of what a parser may accept for consumption.Reading<D,T, A> Constructive pairs of syntax and semantic interpretation.A safe map of keys to initial values.Reading.Item<D,R> A parsing result annotated with diagnostic messages.Reading.Key<A>A key with static type information about its associated value.Reading.Record<D,T, A> Queriable summary of the outcomes of processing input with a reading.Factory with configurable operations for constructing readings.Functional interface for constructing binary AST nodes for operators.Configurable template for binary operator syntax.Framework factories for procedural AST construction.Factory with configurable operations for AST construction.Indicates that the creation of an AST node has been aborted deliberately due to semantic errors in the input.Base of all AST node-constructor functional interfaces.Functional interface for constructors where all syntactic arguments are given, and only context is yet unknown.ReducerFactory.Constructor1<D,A1, R> Functional interface for AST node constructors with one parsed argument.ReducerFactory.Constructor2<D,A1, A2, R> Functional interface for AST node constructors with two parsed arguments.ReducerFactory.Constructor3<D,A1, A2, A3, R> Functional interface for AST node constructors with three parsed arguments.ReducerFactory.Constructor4<D,A1, A2, A3, A4, R> Functional interface for AST node constructors with four parsed arguments.ReducerFactory.Constructor5<D,A1, A2, A3, A4, A5, R> Functional interface for AST node constructors with five parsed arguments.ReducerFactory.Constructor6<D,A1, A2, A3, A4, A5, A6, R> Functional interface for AST node constructors with six parsed arguments.ReducerFactory.Constructor7<D,A1, A2, A3, A4, A5, A6, A7, R> Functional interface for AST node constructors with seven parsed arguments.Immutable semantic parsing state for AST construction.Functional interface for projections that extract data from a state.A data class with no information content.Update<M>Search-tree continuations that make semantic state transitions.