racc 1.6.2 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,48 +15,47 @@ supported: Ruby style (`# ...`) and C style (`/* ... */`).
15
15
  == Class Block
16
16
 
17
17
  The class block is formed like this:
18
- --
19
- class CLASS_NAME
20
- [precedence table]
21
- [token declarations]
22
- [expected number of S/R conflict]
23
- [options]
24
- [semantic value conversion]
25
- [start rule]
26
- rule
27
- GRAMMARS
28
- --
18
+
19
+ class CLASS_NAME
20
+ [precedence table]
21
+ [token declarations]
22
+ [expected number of S/R conflict]
23
+ [options]
24
+ [semantic value conversion]
25
+ [start rule]
26
+ rule
27
+ GRAMMARS
28
+
29
29
  CLASS_NAME is a name of parser class.
30
30
  This is the name of generating parser class.
31
31
 
32
32
  If CLASS_NAME includes '::', Racc outputs module clause.
33
- For example, writing "class M::C" causes creating the code bellow:
34
- --
35
- module M
36
- class C
37
- :
38
- :
33
+ For example, writing "class M::C" causes creating the code below:
34
+
35
+ module M
36
+ class C
37
+ :
38
+ :
39
+ end
39
40
  end
40
- end
41
- --
42
41
 
43
42
  == Grammar Block
44
43
 
45
44
  The grammar block describes the grammar
46
45
  to be understood by parser. Syntax is:
47
- --
48
- (token): (token) (token) (token).... (action)
49
46
 
50
- (token): (token) (token) (token).... (action)
51
- | (token) (token) (token).... (action)
52
- | (token) (token) (token).... (action)
53
- --
47
+ (token): (token) (token) (token).... (action)
48
+
49
+ (token): (token) (token) (token).... (action)
50
+ | (token) (token) (token).... (action)
51
+ | (token) (token) (token).... (action)
52
+
54
53
  (action) is an action which is executed when its (token)s are found.
55
54
  (action) is a ruby code block, which is surrounded by braces:
56
- --
57
- { print val[0]
58
- puts val[1] }
59
- --
55
+
56
+ { print val[0]
57
+ puts val[1] }
58
+
60
59
  Note that you cannot use '%' string, here document, '%r' regexp in action.
61
60
 
62
61
  Actions can be omitted.
@@ -66,20 +65,20 @@ A return value of action is a value of left side value ($$).
66
65
  It is value of result, or returned value by "return" statement.
67
66
 
68
67
  Here is an example of whole grammar block.
69
- --
70
- rule
71
- goal: definition rules source { result = val }
72
-
73
- definition: /* none */ { result = [] }
74
- | definition startdesig { result[0] = val[1] }
75
- | definition
76
- precrule # this line continue from upper line
77
- {
78
- result[1] = val[1]
79
- }
80
-
81
- startdesig: START TOKEN
82
- --
68
+
69
+ rule
70
+ goal: definition rules source { result = val }
71
+
72
+ definition: /* none */ { result = [] }
73
+ | definition startdesig { result[0] = val[1] }
74
+ | definition
75
+ precrule # this line continue from upper line
76
+ {
77
+ result[1] = val[1]
78
+ }
79
+
80
+ startdesig: START TOKEN
81
+
83
82
  You can use following special local variables in action.
84
83
 
85
84
  * result ($$)
@@ -99,45 +98,43 @@ DO NOT MODIFY this stack unless you know what you are doing.
99
98
 
100
99
  This function is equal to '%prec' in yacc.
101
100
  To designate this block:
102
- --
103
- prechigh
104
- nonassoc '++'
105
- left '*' '/'
106
- left '+' '-'
107
- right '='
108
- preclow
109
- --
101
+
102
+ prechigh
103
+ nonassoc '++'
104
+ left '*' '/'
105
+ left '+' '-'
106
+ right '='
107
+ preclow
108
+
110
109
  `right' is yacc's %right, `left' is yacc's %left.
111
110
 
112
111
  `=' + (symbol) means yacc's %prec:
113
- --
114
- prechigh
115
- nonassoc UMINUS
116
- left '*' '/'
117
- left '+' '-'
118
- preclow
119
-
120
- rule
121
- exp: exp '*' exp
122
- | exp '-' exp
123
- | '-' exp =UMINUS # equals to "%prec UMINUS"
124
- :
125
- :
126
- --
112
+
113
+ prechigh
114
+ nonassoc UMINUS
115
+ left '*' '/'
116
+ left '+' '-'
117
+ preclow
118
+
119
+ rule
120
+ exp: exp '*' exp
121
+ | exp '-' exp
122
+ | '-' exp =UMINUS # equals to "%prec UMINUS"
123
+ :
124
+ :
127
125
 
128
126
  == expect
129
127
 
130
128
  Racc supports Bison's "expect" directive to declare the expected
131
129
  number of shift/reduce conflicts.
132
- --
133
- class MyParser
134
- rule
135
- expect 3
136
- :
137
- :
138
- --
139
- Then warnings are issued only when the effective number of conflicts differs.
140
130
 
131
+ class MyParser
132
+ expect 3
133
+ rule
134
+ :
135
+ :
136
+
137
+ Then warnings are issued only when the effective number of conflicts differs.
141
138
 
142
139
  == Declaring Tokens
143
140
 
@@ -145,17 +142,16 @@ Declaring tokens avoids many bugs.
145
142
 
146
143
  Racc outputs warnings for declared tokens that do not exist, or existing tokens not declared.
147
144
  The syntax is:
148
- --
149
- token TOKEN_NAME AND_IS_THIS
150
- ALSO_THIS_IS AGAIN_AND_AGAIN THIS_IS_LAST
151
- --
145
+
146
+ token TOKEN_NAME AND_IS_THIS
147
+ ALSO_THIS_IS AGAIN_AND_AGAIN THIS_IS_LAST
152
148
 
153
149
  == Options
154
150
 
155
151
  You can write options for racc command in your racc file.
156
- --
157
- options OPTION OPTION ...
158
- --
152
+
153
+ options OPTION OPTION ...
154
+
159
155
  Options are:
160
156
 
161
157
  * omit_action_call
@@ -179,32 +175,29 @@ Token symbols are, as default,
179
175
 
180
176
  You can change this default using a "convert" block.
181
177
  Here is an example:
182
- --
183
- convert
184
- PLUS 'PlusClass' # We use PlusClass for symbol of `PLUS'
185
- MIN 'MinusClass' # We use MinusClass for symbol of `MIN'
186
- end
187
- --
178
+
179
+ convert
180
+ PLUS 'PlusClass' # We use PlusClass for symbol of `PLUS'
181
+ MIN 'MinusClass' # We use MinusClass for symbol of `MIN'
182
+ end
183
+
188
184
  We can use almost all ruby value can be used by token symbol,
189
185
  except 'false' and 'nil'. These are causes unexpected parse error.
190
186
 
191
187
  If you want to use String as token symbol, special care is required.
192
188
  For example:
193
- --
194
- convert
195
- class '"cls"' # in code, "cls"
196
- PLUS '"plus\n"' # in code, "plus\n"
197
- MIN "\"minus#{val}\"" # in code, \"minus#{val}\"
198
- end
199
- --
189
+
190
+ convert
191
+ class '"cls"' # in code, "cls"
192
+ PLUS '"plus\n"' # in code, "plus\n"
193
+ MIN "\"minus#{val}\"" # in code, \"minus#{val}\"
194
+ end
200
195
 
201
196
  == Start Rule
202
197
 
203
198
  '%start' in yacc. This changes the start symbol.
204
- --
205
- start real_target
206
- --
207
199
 
200
+ start real_target
208
201
 
209
202
  == User Code Block
210
203
 
@@ -213,14 +206,13 @@ There are three user code blocks, "header" "inner" and "footer".
213
206
 
214
207
  User code blocks are introduced by four '-' at the beginning of a line,
215
208
  followed by a single-word name:
216
- --
217
- ---- header
218
- ruby statement
219
- ruby statement
220
- ruby statement
221
-
222
- ---- inner
223
- ruby statement
224
- :
225
- :
226
- --
209
+
210
+ ---- header
211
+ ruby statement
212
+ ruby statement
213
+ ruby statement
214
+
215
+ ---- inner
216
+ ruby statement
217
+ :
218
+ :
@@ -127,8 +127,8 @@ Racc has bison's "expect" directive.
127
127
  # Example
128
128
 
129
129
  class MyParser
130
- rule
131
130
  expect 3
131
+ rule
132
132
  :
133
133
  :
134
134
 
@@ -6,6 +6,7 @@ racc [-o<var>filename</var>] [--output-file=<var>filename</var>]
6
6
  [-O<var>filename</var>] [--log-file=<var>filename</var>]
7
7
  [-g] [--debug]
8
8
  [-E] [--embedded]
9
+ [-F] [--frozen]
9
10
  [-l] [--no-line-convert]
10
11
  [-c] [--line-convert-all]
11
12
  [-a] [--no-omit-actions]
@@ -50,6 +51,10 @@ Ruby のパスを使用します。
50
51
  ランタイムルーチンをすべて含んだコードを生成します。
51
52
  つまり、このオプションをつけて生成したコードは Ruby さえあれば動きます。
52
53
  </dd>
54
+ <dt>-F, --frozen
55
+ <dd>
56
+ Add frozen_string_literals: true.
57
+ </dd>
53
58
  <dt>-C, --check-only
54
59
  <dd>
55
60
  (文法ファイルの) 文法のチェックだけをして終了します。
@@ -173,7 +173,7 @@ target: TERM_A nonterm_a TERM_B nonterm_b
173
173
  --
174
174
  prechigh
175
175
  nonassoc PLUSPLUS
176
- left MULTI DEVIDE
176
+ left MULTI DIVIDE
177
177
  left PLUS MINUS
178
178
  right '='
179
179
  preclow
@@ -96,7 +96,7 @@ Object
96
96
  このメソッドから正常に戻った場合、パーサはエラー回復モード
97
97
  に移行します。
98
98
 
99
- error_token はパースエラーを起こした記号の内部表現 (整数) です。
99
+ error_token_id はパースエラーを起こした記号の内部表現 (整数) です。
100
100
  #token_to_str で文法ファイル上の文字列表現に直せます。
101
101
 
102
102
  error_value はその値です。
@@ -22,7 +22,7 @@
22
22
  Important Constants
23
23
  ----------------------------------------------------------------------- */
24
24
 
25
- #define RACC_VERSION "1.6.2"
25
+ #define RACC_VERSION STRINGIZE(RACC_INFO_VERSION)
26
26
 
27
27
  #define DEFAULT_TOKEN -1
28
28
  #define ERROR_TOKEN 1
@@ -70,12 +70,8 @@ static ID id_d_e_pop;
70
70
  # define LONG2NUM(i) INT2NUM(i)
71
71
  #endif
72
72
 
73
- #ifndef HAVE_RB_ARY_SUBSEQ
74
- # define rb_ary_subseq(ary, beg, len) rb_ary_new4(len, RARRAY_PTR(ary) + beg)
75
- #endif
76
-
77
- static ID value_to_id _((VALUE v));
78
- static inline long num_to_long _((VALUE n));
73
+ static ID value_to_id(VALUE v);
74
+ static inline long num_to_long(VALUE n);
79
75
 
80
76
  static ID
81
77
  value_to_id(VALUE v)
@@ -99,8 +95,8 @@ num_to_long(VALUE n)
99
95
  Parser Stack Interfaces
100
96
  ----------------------------------------------------------------------- */
101
97
 
102
- static VALUE get_stack_tail _((VALUE stack, long len));
103
- static void cut_stack_tail _((VALUE stack, long len));
98
+ static VALUE get_stack_tail(VALUE stack, long len);
99
+ static void cut_stack_tail(VALUE stack, long len);
104
100
 
105
101
  static VALUE
106
102
  get_stack_tail(VALUE stack, long len)
@@ -189,27 +185,27 @@ struct cparse_params {
189
185
  Parser Main Routines
190
186
  ----------------------------------------------------------------------- */
191
187
 
192
- static VALUE racc_cparse _((VALUE parser, VALUE arg, VALUE sysdebug));
193
- static VALUE racc_yyparse _((VALUE parser, VALUE lexer, VALUE lexmid,
194
- VALUE arg, VALUE sysdebug));
195
-
196
- static void call_lexer _((struct cparse_params *v));
197
- static VALUE lexer_i _((RB_BLOCK_CALL_FUNC_ARGLIST(block_args, data)));
198
-
199
- static VALUE assert_array _((VALUE a));
200
- static long assert_integer _((VALUE n));
201
- static VALUE assert_hash _((VALUE h));
202
- static VALUE initialize_params _((VALUE vparams, VALUE parser, VALUE arg,
203
- VALUE lexer, VALUE lexmid));
204
- static void cparse_params_mark _((void *ptr));
205
- static size_t cparse_params_memsize _((const void *ptr));
206
-
207
- static void parse_main _((struct cparse_params *v,
208
- VALUE tok, VALUE val, int resume));
209
- static void extract_user_token _((struct cparse_params *v,
210
- VALUE block_args, VALUE *tok, VALUE *val));
211
- static void shift _((struct cparse_params* v, long act, VALUE tok, VALUE val));
212
- static int reduce _((struct cparse_params* v, long act));
188
+ static VALUE racc_cparse(VALUE parser, VALUE arg, VALUE sysdebug);
189
+ static VALUE racc_yyparse(VALUE parser, VALUE lexer, VALUE lexmid,
190
+ VALUE arg, VALUE sysdebug);
191
+
192
+ static void call_lexer(struct cparse_params *v);
193
+ static VALUE lexer_i(RB_BLOCK_CALL_FUNC_ARGLIST(block_args, data));
194
+
195
+ static VALUE assert_array(VALUE a);
196
+ static long assert_integer(VALUE n);
197
+ static VALUE assert_hash(VALUE h);
198
+ static VALUE initialize_params(VALUE vparams, VALUE parser, VALUE arg,
199
+ VALUE lexer, VALUE lexmid);
200
+ static void cparse_params_mark(void *ptr);
201
+ static size_t cparse_params_memsize(const void *ptr);
202
+
203
+ static void parse_main(struct cparse_params *v,
204
+ VALUE tok, VALUE val, int resume);
205
+ static void extract_user_token(struct cparse_params *v,
206
+ VALUE block_args, VALUE *tok, VALUE *val);
207
+ static void shift(struct cparse_params* v, long act, VALUE tok, VALUE val);
208
+ static int reduce(struct cparse_params* v, long act);
213
209
  static rb_block_call_func reduce0;
214
210
 
215
211
  #ifdef DEBUG
@@ -278,28 +274,11 @@ racc_yyparse(VALUE parser, VALUE lexer, VALUE lexmid, VALUE arg, VALUE sysdebug)
278
274
  return v->retval;
279
275
  }
280
276
 
281
- #ifdef HAVE_RB_BLOCK_CALL
282
277
  static void
283
278
  call_lexer(struct cparse_params *v)
284
279
  {
285
280
  rb_block_call(v->lexer, v->lexmid, 0, NULL, lexer_i, v->value_v);
286
281
  }
287
- #else
288
- static VALUE
289
- lexer_iter(VALUE data)
290
- {
291
- struct cparse_params *v = rb_check_typeddata(data, &cparse_params_type);
292
-
293
- rb_funcall(v->lexer, v->lexmid, 0);
294
- return Qnil;
295
- }
296
-
297
- static void
298
- call_lexer(struct cparse_params *v)
299
- {
300
- rb_iterate(lexer_iter, v->value_v, lexer_i, v->value_v);
301
- }
302
- #endif
303
282
 
304
283
  static VALUE
305
284
  lexer_i(RB_BLOCK_CALL_FUNC_ARGLIST(block_args, data))
@@ -1,9 +1,8 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  #
3
3
 
4
4
  require 'mkmf'
5
+ require_relative '../../../lib/racc/info'
5
6
 
6
- have_func('rb_block_call')
7
- have_func('rb_ary_subseq')
8
-
7
+ $defs << "-D""RACC_INFO_VERSION=#{Racc::VERSION}"
9
8
  create_makefile 'racc/cparse'
data/lib/racc/grammar.rb CHANGED
@@ -10,11 +10,11 @@
10
10
  #
11
11
  #++
12
12
 
13
- require 'racc/compat'
14
- require 'racc/iset'
15
- require 'racc/sourcetext'
16
- require 'racc/logfilegenerator'
17
- require 'racc/exception'
13
+ require_relative 'compat'
14
+ require_relative 'iset'
15
+ require_relative 'sourcetext'
16
+ require_relative 'logfilegenerator'
17
+ require_relative 'exception'
18
18
  require 'forwardable'
19
19
 
20
20
  module Racc
@@ -27,6 +27,7 @@ module Racc
27
27
  @rules = [] # :: [Rule]
28
28
  @start = nil
29
29
  @n_expected_srconflicts = nil
30
+ @error_on_expect_mismatch = nil
30
31
  @prec_table = []
31
32
  @prec_table_closed = false
32
33
  @closed = false
@@ -36,6 +37,7 @@ module Racc
36
37
  attr_reader :start
37
38
  attr_reader :symboltable
38
39
  attr_accessor :n_expected_srconflicts
40
+ attr_accessor :error_on_expect_mismatch
39
41
 
40
42
  def [](x)
41
43
  @rules[x]
@@ -787,6 +789,81 @@ module Racc
787
789
  end
788
790
 
789
791
 
792
+ class OptionMark
793
+ def initialize(lineno)
794
+ @lineno = lineno
795
+ end
796
+
797
+ def name
798
+ '?'
799
+ end
800
+
801
+ alias inspect name
802
+
803
+ attr_reader :lineno
804
+ end
805
+
806
+
807
+ class ManyMark
808
+ def initialize(lineno)
809
+ @lineno = lineno
810
+ end
811
+
812
+ def name
813
+ '*'
814
+ end
815
+
816
+ alias inspect name
817
+
818
+ attr_reader :lineno
819
+ end
820
+
821
+
822
+ class Many1Mark
823
+ def initialize(lineno)
824
+ @lineno = lineno
825
+ end
826
+
827
+ def name
828
+ '+'
829
+ end
830
+
831
+ alias inspect name
832
+
833
+ attr_reader :lineno
834
+ end
835
+
836
+
837
+ class GroupStartMark
838
+ def initialize(lineno)
839
+ @lineno = lineno
840
+ end
841
+
842
+ def name
843
+ '('
844
+ end
845
+
846
+ alias inspect name
847
+
848
+ attr_reader :lineno
849
+ end
850
+
851
+
852
+ class GroupEndMark
853
+ def initialize(lineno)
854
+ @lineno = lineno
855
+ end
856
+
857
+ def name
858
+ ')'
859
+ end
860
+
861
+ alias inspect name
862
+
863
+ attr_reader :lineno
864
+ end
865
+
866
+
790
867
  class Prec
791
868
  def initialize(symbol, lineno)
792
869
  @symbol = symbol
@@ -993,10 +1070,6 @@ module Racc
993
1070
  @to_s = '$end'
994
1071
  @serialized = 'false'
995
1072
  @string = false
996
- when ErrorSymbolValue
997
- @to_s = 'error'
998
- @serialized = 'Object.new'
999
- @string = false
1000
1073
  else
1001
1074
  raise ArgumentError, "unknown symbol value: #{value.class}"
1002
1075
  end