3 * Copyright (C) 2008-2009 Florian Brosch, Didier Villevalois
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Didier 'Ptitjes Villevalois <ptitjes@free.fr>
24 public abstract class Valadoc
.Rule
: Object
{
26 public static Rule
seq (Object
[] scheme
) {
27 return new
SequenceRule (scheme
);
30 public static Rule
one_of (Object
[] scheme
) {
31 return new
OneOfRule (scheme
);
34 public static Rule
many (Object
[] scheme
) {
35 if (scheme
.length
== 1) {
36 return new
ManyRule (scheme
[0]);
38 return new
ManyRule (new
SequenceRule (scheme
));
42 public static Rule
option (Object
[] scheme
) {
43 if (scheme
.length
== 1) {
44 return new
OptionalRule (scheme
[0]);
46 return new
OptionalRule (new
SequenceRule (scheme
));
53 private string? _name
= null;
54 private Action _start_action
;
55 private Action _reduce_action
;
56 private Action _skip_action
;
58 public string name
{ get { return _name
; } }
60 public Rule
set_name (string name
) {
65 public delegate
void Action () throws ParserError
;
67 public Rule
set_start (Action action
) {
68 //TODO: Ownership Transfer
69 _start_action
= () => { action (); };
73 public Rule
set_reduce (Action action
) {
74 //TODO: Ownership Transfer
75 _reduce_action
= () => { action (); };
79 public Rule
set_skip (Action action
) {
80 //TODO: Ownership Transfer
81 _skip_action
= () => { action (); };
91 public abstract bool is_optional ();
92 public abstract bool starts_with_token (Token token
);
93 public abstract bool accept_token (Token token
, ParserCallback parser
, Rule
.Forward forward
) throws ParserError
;
94 public abstract bool would_accept_token (Token token
, Object? state
);
95 public abstract bool would_reduce (Token token
, Object? state
);
97 public abstract string to_string (Object? state
);
99 protected bool is_optional_rule (Object? scheme_element
) {
100 Rule? scheme_rule
= scheme_element as Rule
;
101 if (scheme_rule
!= null) {
102 return scheme_rule
.is_optional ();
107 protected bool has_start_token (Object? scheme_element
, Token token
) {
108 TokenType? scheme_token_type
= scheme_element as TokenType
;
109 if (scheme_token_type
!= null) {
110 return scheme_token_type
.matches (token
);
112 Rule? scheme_rule
= scheme_element as Rule
;
113 if (scheme_rule
!= null) {
114 return scheme_rule
.starts_with_token (token
);
119 protected bool try_to_apply (Object? scheme_element
, Token token
, ParserCallback parser
,
120 out bool handled
) throws ParserError
124 TokenType? scheme_token
= scheme_element as TokenType
;
125 Rule? scheme_rule
= scheme_element as Rule
;
126 if (scheme_token
!= null) {
127 message ("TryToApply: token='%s'; scheme_token='%s'", token
.to_string (),
128 scheme_token
.to_string ());
129 } else if (scheme_rule
!= null) {
130 message ("TryToApply: token='%s'; scheme_rule='%s'", token
.to_string (),
131 scheme_rule
.to_string (parser
.get_rule_state ()));
133 assert (scheme_element
!= null);
137 TokenType? scheme_token_type
= scheme_element as TokenType
;
138 if (scheme_token_type
!= null && scheme_token_type
.matches (token
)) {
139 scheme_token_type
.do_action (token
);
143 Rule? scheme_rule
= scheme_element as Rule
;
144 if (scheme_rule
!= null && scheme_rule
.starts_with_token (token
)) {
145 parser
.push_rule (scheme_rule
);
154 protected void do_start (ParserCallback parser
) throws ParserError
{
155 if (_start_action
!= null) {
160 protected void do_reduce (ParserCallback parser
) throws ParserError
{
161 if (_reduce_action
!= null) {
167 protected void do_skip (ParserCallback parser
) throws ParserError
{
168 if (_skip_action
!= null) {