lrama 0.6.2 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yaml +2 -3
  3. data/Gemfile +1 -1
  4. data/NEWS.md +101 -1
  5. data/README.md +23 -0
  6. data/Steepfile +5 -0
  7. data/lib/lrama/context.rb +4 -4
  8. data/lib/lrama/grammar/code/destructor_code.rb +40 -0
  9. data/lib/lrama/grammar/code/initial_action_code.rb +6 -0
  10. data/lib/lrama/grammar/code/no_reference_code.rb +4 -0
  11. data/lib/lrama/grammar/code/printer_code.rb +6 -0
  12. data/lib/lrama/grammar/code/rule_action.rb +11 -1
  13. data/lib/lrama/grammar/code.rb +1 -0
  14. data/lib/lrama/grammar/destructor.rb +9 -0
  15. data/lib/lrama/grammar/reference.rb +4 -3
  16. data/lib/lrama/grammar/rule_builder.rb +10 -3
  17. data/lib/lrama/grammar/stdlib.y +42 -0
  18. data/lib/lrama/grammar/symbol.rb +4 -2
  19. data/lib/lrama/grammar/symbols/resolver.rb +293 -0
  20. data/lib/lrama/grammar/symbols.rb +1 -0
  21. data/lib/lrama/grammar.rb +32 -244
  22. data/lib/lrama/lexer/token/user_code.rb +13 -2
  23. data/lib/lrama/lexer/token.rb +1 -1
  24. data/lib/lrama/lexer.rb +7 -0
  25. data/lib/lrama/option_parser.rb +25 -12
  26. data/lib/lrama/options.rb +1 -0
  27. data/lib/lrama/output.rb +75 -2
  28. data/lib/lrama/parser.rb +537 -464
  29. data/lib/lrama/state.rb +4 -4
  30. data/lib/lrama/states/item.rb +6 -8
  31. data/lib/lrama/states_reporter.rb +2 -2
  32. data/lib/lrama/version.rb +1 -1
  33. data/lrama.gemspec +7 -0
  34. data/parser.y +27 -0
  35. data/sig/lrama/grammar/binding.rbs +0 -1
  36. data/sig/lrama/grammar/code/destructor_code.rbs +15 -0
  37. data/sig/lrama/grammar/destructor.rbs +11 -0
  38. data/sig/lrama/grammar/parameterizing_rule/resolver.rbs +0 -1
  39. data/sig/lrama/grammar/reference.rbs +2 -2
  40. data/sig/lrama/grammar/symbol.rbs +5 -4
  41. data/sig/lrama/grammar/symbols/resolver.rbs +41 -0
  42. data/sig/lrama/grammar/type.rbs +11 -0
  43. data/sig/lrama/options.rbs +17 -0
  44. data/template/bison/yacc.c +12 -1
  45. metadata +17 -3
data/lib/lrama/state.rb CHANGED
@@ -29,8 +29,8 @@ module Lrama
29
29
  end
30
30
 
31
31
  def non_default_reduces
32
- reduces.select do |reduce|
33
- reduce.rule != @default_reduction_rule
32
+ reduces.reject do |reduce|
33
+ reduce.rule == @default_reduction_rule
34
34
  end
35
35
  end
36
36
 
@@ -105,8 +105,8 @@ module Lrama
105
105
  end
106
106
 
107
107
  def selected_term_transitions
108
- term_transitions.select do |shift, next_state|
109
- !shift.not_selected
108
+ term_transitions.reject do |shift, next_state|
109
+ shift.not_selected
110
110
  end
111
111
  end
112
112
 
@@ -1,8 +1,14 @@
1
1
  # TODO: Validate position is not over rule rhs
2
2
 
3
+ require "forwardable"
4
+
3
5
  module Lrama
4
6
  class States
5
7
  class Item < Struct.new(:rule, :position, keyword_init: true)
8
+ extend Forwardable
9
+
10
+ def_delegators "rule", :lhs, :rhs
11
+
6
12
  # Optimization for States#setup_state
7
13
  def hash
8
14
  [rule_id, position].hash
@@ -20,14 +26,6 @@ module Lrama
20
26
  rhs.count - position
21
27
  end
22
28
 
23
- def lhs
24
- rule.lhs
25
- end
26
-
27
- def rhs
28
- rule.rhs
29
- end
30
-
31
29
  def next_sym
32
30
  rhs[position]
33
31
  end
@@ -109,8 +109,8 @@ module Lrama
109
109
  io << "\n"
110
110
 
111
111
  # Report shifts
112
- tmp = state.term_transitions.select do |shift, _|
113
- !shift.not_selected
112
+ tmp = state.term_transitions.reject do |shift, _|
113
+ shift.not_selected
114
114
  end.map do |shift, next_state|
115
115
  [shift.next_sym, next_state.id]
116
116
  end
data/lib/lrama/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lrama
2
- VERSION = "0.6.2".freeze
2
+ VERSION = "0.6.4".freeze
3
3
  end
data/lrama.gemspec CHANGED
@@ -16,6 +16,13 @@ Gem::Specification.new do |spec|
16
16
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
17
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
18
18
  end
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = spec.homepage
22
+ spec.metadata["documentation_uri"] = spec.homepage
23
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/releases"
24
+ spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
25
+
19
26
  spec.bindir = "exe"
20
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
28
  spec.require_paths = ["lib"]
data/parser.y CHANGED
@@ -97,6 +97,13 @@ rule
97
97
  end_c_declaration
98
98
  }
99
99
  "}" generic_symlist
100
+ {
101
+ @grammar.add_destructor(
102
+ ident_or_tags: val[6],
103
+ token_code: val[3],
104
+ lineno: val[3].line
105
+ )
106
+ }
100
107
  | "%printer" "{"
101
108
  {
102
109
  begin_c_declaration("}")
@@ -129,6 +136,26 @@ rule
129
136
  lineno: val[3].line
130
137
  )
131
138
  }
139
+ | "%after-shift" IDENTIFIER
140
+ {
141
+ @grammar.after_shift = val[1]
142
+ }
143
+ | "%before-reduce" IDENTIFIER
144
+ {
145
+ @grammar.before_reduce = val[1]
146
+ }
147
+ | "%after-reduce" IDENTIFIER
148
+ {
149
+ @grammar.after_reduce = val[1]
150
+ }
151
+ | "%after-shift-error-token" IDENTIFIER
152
+ {
153
+ @grammar.after_shift_error_token = val[1]
154
+ }
155
+ | "%after-pop-stack" IDENTIFIER
156
+ {
157
+ @grammar.after_pop_stack = val[1]
158
+ }
132
159
 
133
160
  symbol_declaration: "%token" token_declarations
134
161
  | "%type" symbol_declarations
@@ -4,7 +4,6 @@ module Lrama
4
4
  attr_reader actual_args: Array[Lexer::Token]
5
5
  attr_reader count: Integer
6
6
 
7
- @rule_name: String
8
7
  @required_parameters_count: Integer
9
8
  @parameters: Array[Lexer::Token]
10
9
  @parameter_to_arg: untyped
@@ -0,0 +1,15 @@
1
+ module Lrama
2
+ class Grammar
3
+ class Code
4
+ class DestructorCode < Code
5
+ @tag: untyped
6
+ def initialize: (type: untyped, token_code: untyped, tag: untyped) -> void
7
+
8
+ private
9
+
10
+ # ref: Lrama::Grammar::Code.token_code.references
11
+ def reference_to_c: (untyped ref) -> untyped
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Lrama
2
+ class Grammar
3
+ class Destructor
4
+ attr_accessor ident_or_tags: Array[Lexer::Token::Ident|Lexer::Token::Tag]
5
+ attr_accessor token_code: Grammar::Code
6
+ attr_accessor lineno: Integer
7
+
8
+ def translated_code: (Lexer::Token member) -> String
9
+ end
10
+ end
11
+ end
@@ -8,7 +8,6 @@ module Lrama
8
8
 
9
9
  def initialize: () -> void
10
10
  def add_parameterizing_rule: (Grammar::ParameterizingRule::Rule rule) -> void
11
- def defined?: (Lexer::Token::InstantiateRule token) -> bool
12
11
  def find: (Lexer::Token::InstantiateRule token) -> Grammar::ParameterizingRule::Rule?
13
12
  def created_lhs: (String lhs_s_value) -> Lexer::Token?
14
13
 
@@ -3,6 +3,7 @@ module Lrama
3
3
  class Reference
4
4
  attr_accessor type: ::Symbol
5
5
  attr_accessor name: String
6
+ attr_accessor number: Integer
6
7
  attr_accessor index: Integer
7
8
  attr_accessor ex_tag: Lexer::Token?
8
9
  attr_accessor first_column: Integer
@@ -10,13 +11,12 @@ module Lrama
10
11
  attr_accessor position_in_rhs: Integer?
11
12
 
12
13
  def initialize: (
13
- type: ::Symbol, ?name: String, ?index: Integer, ?ex_tag: Lexer::Token?,
14
+ type: ::Symbol, ?name: String, ?number: Integer, ?index: Integer, ?ex_tag: Lexer::Token?,
14
15
  first_column: Integer, last_column: Integer,
15
16
  ?position_in_rhs: Integer?
16
17
  ) -> void
17
18
 
18
19
  def value: () -> (String|Integer)
19
- def tag: () -> untyped
20
20
  end
21
21
  end
22
22
  end
@@ -2,7 +2,7 @@ module Lrama
2
2
  class Grammar
3
3
  class Symbol
4
4
  attr_accessor id: Lexer::Token
5
- attr_accessor alias_name: String
5
+ attr_accessor alias_name: String?
6
6
  attr_accessor number: Integer
7
7
  attr_accessor tag: Lexer::Token?
8
8
  attr_accessor term: bool
@@ -10,6 +10,7 @@ module Lrama
10
10
  attr_accessor nullable: bool
11
11
  attr_accessor precedence: Precedence?
12
12
  attr_accessor printer: Printer?
13
+ attr_accessor destructor: Destructor?
13
14
  attr_accessor error_token: ErrorToken
14
15
 
15
16
  attr_accessor first_set: Set[Array[Symbol]]
@@ -20,8 +21,8 @@ module Lrama
20
21
  attr_writer accept_symbol: Symbol
21
22
 
22
23
  def initialize: (
23
- id: Lexer::Token, alias_name: String?, number: Integer?, tag: Lexer::Token?,
24
- term: bool, token_id: Integer?, nullable: bool?, precedence: Precedence?, printer: Printer?) -> void
24
+ id: Lexer::Token, term: bool, ?alias_name: String?, ?number: Integer?, ?tag: Lexer::Token?,
25
+ ?token_id: Integer?, ?nullable: bool?, ?precedence: Precedence?, ?printer: Printer?) -> void
25
26
 
26
27
  def term?: () -> bool
27
28
  def nterm?: () -> bool
@@ -31,7 +32,7 @@ module Lrama
31
32
  def accept_symbol?: () -> bool
32
33
  def display_name: () -> String
33
34
  def enum_name: () -> String
34
- def comment: () -> String
35
+ def comment: () -> String?
35
36
  end
36
37
  end
37
38
  end
@@ -0,0 +1,41 @@
1
+ module Lrama
2
+ class Grammar
3
+ class Symbols
4
+ class Resolver
5
+ attr_reader terms: Array[Grammar::Symbol]
6
+ attr_reader nterms: Array[Grammar::Symbol]
7
+
8
+ @symbols: Array[Grammar::Symbol]?
9
+ @number: Integer
10
+ @used_numbers: Hash[Integer, bool]
11
+
12
+ def initialize: () -> void
13
+ def symbols: () -> Array[Grammar::Symbol]
14
+ def sort_by_number!: () -> void
15
+ def add_term: (id: Lexer::Token, ?alias_name: String?, ?tag: Lexer::Token?, ?token_id: Integer?, ?replace: bool) -> Grammar::Symbol
16
+ def add_nterm: (id: Lexer::Token, ?alias_name: String?, ?tag: Lexer::Token?) -> Grammar::Symbol?
17
+ def find_symbol_by_s_value: (Grammar::Symbol s_value) -> Grammar::Symbol?
18
+ def find_symbol_by_s_value!: (Grammar::Symbol s_value) -> Grammar::Symbol
19
+ def find_symbol_by_id: (Lexer::Token id) -> Grammar::Symbol?
20
+ def find_symbol_by_id!: (Lexer::Token id) -> Grammar::Symbol
21
+ def find_symbol_by_token_id: (Integer token_id) -> Grammar::Symbol?
22
+ def find_symbol_by_number!: (Integer number) -> Grammar::Symbol
23
+ def fill_symbol_number: () -> void
24
+ def fill_nterm_type: (Array[Grammar::Type] types) -> void
25
+ def fill_printer: (Array[Grammar::Printer] printers) -> void
26
+ def fill_error_token: (Array[Grammar::ErrorToken] error_tokens) -> void
27
+ def token_to_symbol: (Lexer::Token token) -> Grammar::Symbol
28
+ def validate!: () -> void
29
+
30
+ private
31
+
32
+ def find_nterm_by_id!: (Lexer::Token id) -> Grammar::Symbol
33
+ def fill_terms_number: () -> void
34
+ def fill_nterms_number: () -> void
35
+ def used_numbers: () -> Hash[Integer, bool]
36
+ def validate_number_uniqueness!: () -> void
37
+ def validate_alias_name_uniqueness!: () -> void
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,11 @@
1
+ module Lrama
2
+ class Grammar
3
+ class Type
4
+ attr_reader id: Lexer::Token
5
+ attr_reader tag: Lexer::Token
6
+
7
+ def initialize: (id: Lexer::Token, tag: Lexer::Token) -> void
8
+ def ==: (Grammar::Type other) -> bool
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Lrama
2
+ class Options
3
+ attr_accessor skeleton: String
4
+ attr_accessor header: bool
5
+ attr_accessor header_file: String?
6
+ attr_accessor report_file: String?
7
+ attr_accessor outfile: String
8
+ attr_accessor error_recovery: bool
9
+ attr_accessor grammar_file: String?
10
+ attr_accessor trace_opts: Hash[Symbol, bool]?
11
+ attr_accessor report_opts: Hash[Symbol, bool]?
12
+ attr_accessor y: IO
13
+ attr_accessor debug: bool
14
+
15
+ def initialize: () -> void
16
+ end
17
+ end
@@ -1145,7 +1145,12 @@ yydestruct (const char *yymsg,
1145
1145
  YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp<%= output.user_args %>);
1146
1146
 
1147
1147
  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1148
- YY_USE (yykind);
1148
+ switch (yykind)
1149
+ {
1150
+ <%= output.symbol_actions_for_destructor -%>
1151
+ default:
1152
+ break;
1153
+ }
1149
1154
  YY_IGNORE_MAYBE_UNINITIALIZED_END
1150
1155
  }
1151
1156
 
@@ -1752,6 +1757,7 @@ yybackup:
1752
1757
  *++yyvsp = yylval;
1753
1758
  YY_IGNORE_MAYBE_UNINITIALIZED_END
1754
1759
  *++yylsp = yylloc;
1760
+ <%= output.after_shift_function("/* %after-shift code. */") %>
1755
1761
 
1756
1762
  /* Discard the shifted token. */
1757
1763
  yychar = YYEMPTY;
@@ -1784,6 +1790,7 @@ yyreduce:
1784
1790
  unconditionally makes the parser a bit smaller, and it avoids a
1785
1791
  GCC warning that YYVAL may be used uninitialized. */
1786
1792
  yyval = yyvsp[1-yylen];
1793
+ <%= output.before_reduce_function("/* %before-reduce function. */") %>
1787
1794
 
1788
1795
  /* Default location. */
1789
1796
  YYLLOC_DEFAULT (yyloc, (yylsp - yylen), yylen);
@@ -1809,6 +1816,7 @@ yyreduce:
1809
1816
  YY_SYMBOL_PRINT ("-> $$ =", YY_CAST (yysymbol_kind_t, yyr1[yyn]), &yyval, &yyloc<%= output.user_args %>);
1810
1817
 
1811
1818
  YYPOPSTACK (yylen);
1819
+ <%= output.after_reduce_function("/* %after-reduce function. */") %>
1812
1820
  yylen = 0;
1813
1821
 
1814
1822
  *++yyvsp = yyval;
@@ -1910,6 +1918,7 @@ yyerrorlab:
1910
1918
  /* Do not reclaim the symbols of the rule whose action triggered
1911
1919
  this YYERROR. */
1912
1920
  YYPOPSTACK (yylen);
1921
+ <%= output.after_pop_stack_function("yylen", "/* %after-pop-stack function. */") %>
1913
1922
  yylen = 0;
1914
1923
  YY_STACK_PRINT (yyss, yyssp<%= output.user_args %>);
1915
1924
  yystate = *yyssp;
@@ -1969,6 +1978,7 @@ yyerrlab1:
1969
1978
  yydestruct ("Error: popping",
1970
1979
  YY_ACCESSING_SYMBOL (yystate), yyvsp, yylsp<%= output.user_args %>);
1971
1980
  YYPOPSTACK (1);
1981
+ <%= output.after_pop_stack_function(1, "/* %after-pop-stack function. */") %>
1972
1982
  yystate = *yyssp;
1973
1983
  YY_STACK_PRINT (yyss, yyssp<%= output.user_args %>);
1974
1984
  }
@@ -1983,6 +1993,7 @@ yyerrlab1:
1983
1993
 
1984
1994
  /* Shift the error token. */
1985
1995
  YY_SYMBOL_PRINT ("Shifting", YY_ACCESSING_SYMBOL (yyn), yyvsp, yylsp<%= output.user_args %>);
1996
+ <%= output.after_shift_error_token_function("/* %after-shift-error-token code. */") %>
1986
1997
 
1987
1998
  yystate = yyn;
1988
1999
  goto yynewstate;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lrama
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuichiro Kaneko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-27 00:00:00.000000000 Z
11
+ date: 2024-03-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: LALR (1) parser generator written by Ruby
14
14
  email:
@@ -50,11 +50,13 @@ files:
50
50
  - lib/lrama/grammar/auxiliary.rb
51
51
  - lib/lrama/grammar/binding.rb
52
52
  - lib/lrama/grammar/code.rb
53
+ - lib/lrama/grammar/code/destructor_code.rb
53
54
  - lib/lrama/grammar/code/initial_action_code.rb
54
55
  - lib/lrama/grammar/code/no_reference_code.rb
55
56
  - lib/lrama/grammar/code/printer_code.rb
56
57
  - lib/lrama/grammar/code/rule_action.rb
57
58
  - lib/lrama/grammar/counter.rb
59
+ - lib/lrama/grammar/destructor.rb
58
60
  - lib/lrama/grammar/error_token.rb
59
61
  - lib/lrama/grammar/parameterizing_rule.rb
60
62
  - lib/lrama/grammar/parameterizing_rule/resolver.rb
@@ -68,6 +70,8 @@ files:
68
70
  - lib/lrama/grammar/rule_builder.rb
69
71
  - lib/lrama/grammar/stdlib.y
70
72
  - lib/lrama/grammar/symbol.rb
73
+ - lib/lrama/grammar/symbols.rb
74
+ - lib/lrama/grammar/symbols/resolver.rb
71
75
  - lib/lrama/grammar/type.rb
72
76
  - lib/lrama/grammar/union.rb
73
77
  - lib/lrama/lexer.rb
@@ -109,8 +113,10 @@ files:
109
113
  - sig/lrama/grammar.rbs
110
114
  - sig/lrama/grammar/binding.rbs
111
115
  - sig/lrama/grammar/code.rbs
116
+ - sig/lrama/grammar/code/destructor_code.rbs
112
117
  - sig/lrama/grammar/code/printer_code.rbs
113
118
  - sig/lrama/grammar/counter.rbs
119
+ - sig/lrama/grammar/destructor.rbs
114
120
  - sig/lrama/grammar/error_token.rbs
115
121
  - sig/lrama/grammar/parameterizing_rule.rbs
116
122
  - sig/lrama/grammar/parameterizing_rule/resolver.rbs
@@ -123,6 +129,8 @@ files:
123
129
  - sig/lrama/grammar/rule.rbs
124
130
  - sig/lrama/grammar/rule_builder.rbs
125
131
  - sig/lrama/grammar/symbol.rbs
132
+ - sig/lrama/grammar/symbols/resolver.rbs
133
+ - sig/lrama/grammar/type.rbs
126
134
  - sig/lrama/lexer/grammar_file.rbs
127
135
  - sig/lrama/lexer/location.rbs
128
136
  - sig/lrama/lexer/token.rbs
@@ -131,6 +139,7 @@ files:
131
139
  - sig/lrama/lexer/token/instantiate_rule.rbs
132
140
  - sig/lrama/lexer/token/tag.rbs
133
141
  - sig/lrama/lexer/token/user_code.rbs
142
+ - sig/lrama/options.rbs
134
143
  - sig/lrama/report/duration.rbs
135
144
  - sig/lrama/report/profile.rbs
136
145
  - sig/lrama/warning.rbs
@@ -141,7 +150,12 @@ files:
141
150
  homepage: https://github.com/ruby/lrama
142
151
  licenses:
143
152
  - GPL-3.0-or-later
144
- metadata: {}
153
+ metadata:
154
+ homepage_uri: https://github.com/ruby/lrama
155
+ source_code_uri: https://github.com/ruby/lrama
156
+ documentation_uri: https://github.com/ruby/lrama
157
+ changelog_uri: https://github.com/ruby/lrama/releases
158
+ bug_tracker_uri: https://github.com/ruby/lrama/issues
145
159
  post_install_message:
146
160
  rdoc_options: []
147
161
  require_paths: