Skip to content

Commit

Permalink
add more state checks to the tests suite (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
iliabylich authored Dec 21, 2020
1 parent a7c638b commit fb5206b
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/parser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def self.setup_source_buffer(file, line, string, encoding)
end
private_class_method :setup_source_buffer

attr_reader :lexer
attr_reader :diagnostics
attr_reader :builder
attr_reader :static_env
Expand Down
4 changes: 4 additions & 0 deletions lib/parser/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def reset
@stack.clear
end

def empty?
@stack.empty?
end

def in_class?
@stack.last == :class
end
Expand Down
7 changes: 5 additions & 2 deletions lib/parser/current_arg_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ def initialize
freeze
end

def empty?
@stack.size == 0
end

def push(value)
@stack << value
end

def set(value)
pop
push(value)
@stack[@stack.length - 1] = value
end

def pop
Expand Down
2 changes: 2 additions & 0 deletions lib/parser/lexer.rl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class Parser::Lexer

attr_accessor :tokens, :comments

attr_reader :paren_nest, :cmdarg_stack, :cond_stack, :lambda_stack

def initialize(version)
@version = version
@static_env = nil
Expand Down
16 changes: 12 additions & 4 deletions lib/parser/max_numparam_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@

module Parser

# Holds p->max_numparam from parse.y
#
# @api private
class MaxNumparamStack
attr_reader :stack

ORDINARY_PARAMS = -1

def initialize
@stack = []
end

def empty?
@stack.size == 0
end

def has_ordinary_params!
set(-1)
set(ORDINARY_PARAMS)
end

def has_ordinary_params?
top < 0
top == ORDINARY_PARAMS
end

def has_numparams?
Expand All @@ -40,8 +49,7 @@ def pop
private

def set(value)
@stack.pop
@stack.push(value)
@stack[@stack.length - 1] = value
end
end

Expand Down
17 changes: 15 additions & 2 deletions lib/parser/ruby27.y
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ preclow

rule

program: top_compstmt
program: {
@current_arg_stack.push(nil)
@max_numparam_stack.push
}
top_compstmt
{
result = val[1]

@current_arg_stack.pop
@max_numparam_stack.pop
}

top_compstmt: top_stmts opt_terms
{
Expand Down Expand Up @@ -287,13 +297,14 @@ rule
{
@lexer.state = :expr_beg
@lexer.command_start = false
pattern_variables.push
@pattern_variables.push

result = @lexer.in_kwarg
@lexer.in_kwarg = true
}
p_expr
{
@pattern_variables.pop
@lexer.in_kwarg = val[2]
if @builder.class.emit_match_pattern
result = @builder.match_pattern(val[0], val[1], val[3])
Expand Down Expand Up @@ -1760,6 +1771,8 @@ opt_block_args_tail:
}
p_top_expr then
{
@pattern_hash_keys.pop
@pattern_variables.pop
@lexer.in_kwarg = val[1]
}
compstmt p_cases
Expand Down
22 changes: 18 additions & 4 deletions lib/parser/ruby30.y
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ preclow

rule

program: top_compstmt
program: {
@current_arg_stack.push(nil)
@max_numparam_stack.push
}
top_compstmt
{
result = val[1]

@current_arg_stack.pop
@max_numparam_stack.pop
}

top_compstmt: top_stmts opt_terms
{
Expand Down Expand Up @@ -288,27 +298,29 @@ rule
{
@lexer.state = :expr_beg
@lexer.command_start = false
pattern_variables.push
@pattern_variables.push

result = @lexer.in_kwarg
@lexer.in_kwarg = true
}
p_expr
{
@pattern_variables.pop
@lexer.in_kwarg = val[2]
result = @builder.match_pattern(val[0], val[1], val[3])
}
| arg kIN
{
@lexer.state = :expr_beg
@lexer.command_start = false
pattern_variables.push
@pattern_variables.push

result = @lexer.in_kwarg
@lexer.in_kwarg = true
}
p_expr
{
@pattern_variables.pop
@lexer.in_kwarg = val[2]
result = @builder.match_pattern_p(val[0], val[1], val[3])
}
Expand All @@ -325,7 +337,7 @@ rule
result = [ val[1], val[2] ]
}

def_name: fname
def_name: fname
{
@static_env.extend_static
@lexer.cmdarg.push(false)
Expand Down Expand Up @@ -1852,6 +1864,8 @@ opt_block_args_tail:
}
p_top_expr then
{
@pattern_variables.pop
@pattern_hash_keys.pop
@lexer.in_kwarg = val[1]
}
compstmt p_cases
Expand Down
4 changes: 4 additions & 0 deletions lib/parser/static_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def declare_forward_args
def declared_forward_args?
declared?(FORWARD_ARGS)
end

def empty?
@stack.empty?
end
end

end
4 changes: 4 additions & 0 deletions lib/parser/variables_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def initialize
push
end

def empty?
@stack.empty?
end

def push
@stack << Set.new
end
Expand Down
26 changes: 21 additions & 5 deletions test/parse_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ def try_parsing(ast, code, parser, source_maps, version)
assert_source_range(range, found_range, version, line.inspect)
end

assert parser.instance_eval { @lexer }.cmdarg.empty?,
"(#{version}) expected cmdarg to be empty after parsing"

assert_equal 0, parser.instance_eval { @lexer.instance_eval { @paren_nest } },
"(#{version}) expected paren_nest to be 0 after parsing"
assert_state_is_final(parser, version)
end

# Use like this:
Expand Down Expand Up @@ -343,4 +339,24 @@ def find_matching_nodes(ast, &block)

result
end

def assert_state_is_final(parser, version)
lexer = parser.lexer

assert lexer.cmdarg.empty?, "(#{version}) expected cmdarg to be empty after parsing"
assert lexer.cond.empty?, "(#{version}) expected cond to be empty after parsing"

assert lexer.cmdarg_stack.empty?, "(#{version}) expected cmdarg_stack to be empty after parsing"
assert lexer.cond_stack.empty?, "(#{version}) expected cond_stack to be empty after parsing"

assert_equal 0, lexer.paren_nest, "(#{version}) expected paren_nest to be 0 after parsing"
assert lexer.lambda_stack.empty?, "(#{version}) expected lambda_stack to be empty after parsing"

assert parser.static_env.empty?, "(#{version}) expected static_env to be empty after parsing"
assert parser.context.empty?, "(#{version}) expected context to be empty after parsing"
assert parser.max_numparam_stack.empty?, "(#{version}) expected max_numparam_stack to be empty after parsing"
assert parser.current_arg_stack.empty?, "(#{version}) expected current_arg_stack to be empty after parsing"
assert parser.pattern_variables.empty?, "(#{version}) expected pattern_variables to be empty after parsing"
assert parser.pattern_hash_keys.empty?, "(#{version}) expected pattern_hash_keys to be empty after parsing"
end
end

0 comments on commit fb5206b

Please sign in to comment.