Represents a grammar for structural parsing.
sealed class
|
Grammar
|
extends
|
Disposable
|
The following code shows the grammar for defining grammars:
grammar := (decl-skip | decl-rule)+ ; decl-rule := [!?] <! identifier ':=' rule ';' ; decl-skip := '#' ':=' rule ';' ; rule := rule-list ; rule-ast := '{' identifier ':' rule (('<!' | '!>') rule)? '}' ; rule-brace := '(' rule ')' ; rule-choice := rule-sequence !> ('|' rule-sequence)+ ; rule-except := rule-not !> '\\' rule-not ; rule-list := rule-choice !> '..' rule-choice ; rule-not := '~' <! rule-primary ; rule-primary := rule-char | rule-token | rule-name | rule-brace | rule-ast ; rule-repeat := rule-except !> '?' | '*' | '+' | '[' number ('..' number)? ']' ; rule-sequence := rule-trim !> (rule-trim)+ ; rule-trim := '#' <! rule-repeat !> '#' ; rule-char := char-range | char-set | char-set-inv | char-eof ; char-range := char-one !> '..' char-one ; char-one := '\'' (char-esc | ]\\'[) '\'' ; char-set := '[' (char-esc | ]\]\\[)+ ']' ; char-set-inv := ']' (char-esc | ]\[\\[)+ '[' ; rule-token := '@' <! '\'' (char-esc | ]\\'[)* '\'' ; char-esc := '\\' ([t0\]nr\[\\'] | 'x' hexdigit[1..4]) ; rule-name := identifier ; hexdigit := digit | 'a'..'f' | 'A'..'F' ; identifier := letter (letter | digit)* ; number := digit+ ; char-eof := @'<eof>' ; digit := '0'..'9' ; letter := 'a'..'z' | 'A'..'Z' | [_-] ;The above grammar describes itself in its very own grammar language, so it is by definition fully self-explanatory (but still non-comprehensible, so the documentation goes on).
A grammar contains one or more declarations, which can be one of the following:
decl-rule
'!'
or
'?'
,
which
assigns
an
additional
flag
to
the
grammar
rule:
either
ReportErrorRoot
or
ReportErrorTerminal,
respectively.
! expression := 'A' ; ? identifier := 'B' ; some_rule := 'C' ;
decl-skip
rule-trim
.
# := ' ' ;
rule-list
(lowest
precedence)n
times,
the
right-side
rule
will
always
be
matched
n-1
times.
('A' | 'B' .. # ',' #)
rule-choice
'A' | 'B' | 'C'
rule-sequence
'A' 'B' 'C'
rule-trim
'#'
is
left
of
rule)
and/or
trailing
('#'
is
right
of
rule)
whitespace
characters
(see
IsWhitespace)
from
the
input
before
matching
the
given
rule:
'A' # ',' # 'B' # ',' # 'C'A grammar may optionally define a special skip rule using the special name
'#'
:
the
skip
rule
is
applied
at
all
potential
trim
locations.
All
input
characters
that
are
matched
by
the
skip
rule
are
silently
dropped.
rule-repeat
'?'
zero
or
one,
'*'
zero
or
more,
'+'
one
or
more).
The
minimum
and
maximum
number
of
occurrences
can
be
specified
with
'min..max'
where
min
and
max
specify
the
respective
occurrence
count.
'A'? 'A'* 'A'+ 'A'[2..4]
rule-except
'a' .. 'z' \ 'e'
rule-not
~'A'
rule-primary
(highest
precedence)rule-ast
'!>'
and
'<!'
tokens
are
conditional
operators:
a
syntax
tree
node
is
created
only
iff
the
rules
to
the
right
resp.
to
the
left
are
also
matched.
{ AST : 'A' } { AST : 'A' !> 'B' } { AST : 'A' <! 'B' }
rule-brace
a (b | c) d (a | b) (c | d)
rule-char
'A' 'A'..'C' [ABC] ]ABC[ <eof>Inverse brackets
']'
and
'['
indicate
that
all
characters
except
the
specified
ones
will
be
matched
(inverse
set).
The
<eof>
token
will
match
the
end
of
input
condition
(see
IsEof).
rule-token
'@'
prefix
to
match
case-insensitively.
'keyword_case_sensitive' @'KeyWord_Case_InSensitive'
rule-name
name-of-grammar-rule rule_123
Parsing is performed by the following steps.
Type for default root AST nodes.
public
constant
|
AstRoot
=
"ROOT"
|
||
type
|
string
|
Remarks:
When the result of a parsing process yielded more than one root AST node, an artificial root node of this type is created that holds the yielded nodes.
Returns the source code for grammars in the grammar language.
public
static
property
|
GrammarCode
{
get
}
|
||
type
|
string
|
||
value
|
|
The grammar source code. |
Returns the lifecycle state of this object.
public
virtual
property
|
LifecycleState
{
get
}
|
||
type
|
LifecycleState
|
||
value
|
The lifecycle state. | ||
inherited
|
Disposable.LifecycleState
|
The grammar rules.
public
property
|
Rules
{
get
}
|
||
type
|
IMapConst<string, IGrammarRule>
|
||
value
|
|
Mapping from grammar rule name (see RuleName) to grammar rules. |
Creates a new Grammar object.
[OwnerReturn]
|
||||
public
static
method
|
ParseGrammar
(string source)
|
|||
type
|
GrammarBuilder
|
|||
params
|
source
|
[not-empty]
|
The grammar source code. | |
returns
|
|
The GrammarBuilder object for building the grammar. Call Build to get the final Grammar object. |
Creates a new Grammar object.
[OwnerReturn]
|
||||
public
static
method
|
ParseGrammar
(int32 lockedSource)
|
|||
type
|
GrammarBuilder
|
|||
params
|
lockedSource
|
The locked grammar source code. | ||
returns
|
|
The GrammarBuilder object for building the grammar. Call Build to get the final Grammar object. |
Acquires a strong reference to this disposable object.
[OwnerReturn, ThreadSafe]
|
||||
public
method
|
AcquireTry
()
|
|||
type
|
IDisposable
|
|||
returns
|
this
if
a
new
strong
reference
has
been
acquired,
null
if
this
object
is
already
being
disposed.
|
|||
inherited
|
Disposable.AcquireTry
|
Remarks:
The object will not be actually disposed by calls to Dispose when there is at least one strong reference left. Code that calls the AcquireTry method is responsible for calling the Dispose method accordingly.
This method is not intended to be used in performance-critical code. It should only be used to high-level resource management.
Releases all resources held by this object if there are no more strong references to it, decrements the reference counter by one otherwise.
[Dispose, OwnerThis, ThreadSafe]
|
||||
public
method
|
Dispose
()
|
|||
inherited
|
Disposable.Dispose
|
Remarks:
The Dispose method silently returns if the object has already been disposed.
Returns a rule of this grammar by its name.
public
method
|
GetRule
(string name = null)
|
||
type
|
IGrammarRule
|
||
params
|
name
|
The
rule
name
or
null
to
return
the
default
rule. |
|
returns
|
The
rule
or
null
if
no
rule
of
the
given
name
exists. |
Parses the given source code using the specified grammar rule.
public
method
|
ParseAst
(string input,
string rule = null)
|
||
type
|
AstNode
|
||
params
|
input
|
[not-null]
|
The source code to parse. |
rule
|
The
rule
name
or
null
to
use
the
default
rule. |
||
returns
|
|
The resulting AST. |
See also:
AstNode.CreatePsiParses the given source code using the specified grammar rule.
public
method
|
ParseAst
(ICodeInput input,
string rule = null)
|
||
type
|
AstNode
|
||
params
|
input
|
[not-null]
|
The source code to parse. |
rule
|
The
rule
name
or
null
to
use
the
default
rule. |
||
returns
|
|
The resulting AST. |
See also:
AstNode.CreatePsiParses the given input data using the specified rule.
public
method
|
ParseInfo
(string input,
string rule = null)
|
||
type
|
ParseResult
|
||
params
|
input
|
[not-null]
|
The input data. |
rule
|
The
rule
name
or
null
to
use
the
default
rule. |
||
returns
|
|
The parser result object. |
See also:
ParseResult.AstParses the given input data using the specified rule, returning a status object that contains additional information about the parsing process.
public
method
|
ParseInfo
(ICodeInput input,
string rule = null)
|
||
type
|
ParseResult
|
||
params
|
input
|
[not-null]
|
The input data. |
rule
|
The
rule
name
or
null
to
use
the
default
rule. |
||
returns
|
|
The parser result object. |
See also:
ParseResult.AstParses the given source code using the specified grammar rule.
public
method
|
ParsePsi
(string input,
string rule = null)
|
||
type
|
object
|
||
params
|
input
|
[not-null]
|
The source code to parse. |
rule
|
The
rule
name
or
null
to
use
the
default
rule. |
||
returns
|
|
The resulting AST. |
Parses the given source code using the specified grammar rule.
public
method
|
ParsePsi
(ICodeInput input,
string rule = null)
|
||
type
|
object
|
||
params
|
input
|
[not-null]
|
The source code to parse. |
rule
|
The
rule
name
or
null
to
use
the
default
rule. |
||
returns
|
|
The resulting AST. |
Parses the given input, using the specified grammar.
public
static
method
|
ParseWith
(string grammar,
string input)
|
||
type
|
AstNode
|
||
params
|
grammar
|
[not-empty]
|
The grammar to use. |
input
|
[not-empty]
|
The input to parse. | |
returns
|
|
The parsed AST. |
Exceptions:
Returns the grammar source code.
public
method
|
ToSource
(RuleToSourceFlags flags = RuleToSourceFlags.None)
|
||
type
|
string
|
||
params
|
flags
|
The format flags to use. | |
returns
|
|
The grammar source code. |