Skip to content

Commit

Permalink
- Fixed 4/5 of literal lambda tests (jamie)
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/ruby_parser/dev/": change = 6689]
  • Loading branch information
zenspider committed Oct 2, 2011
1 parent bef6d9c commit 53d52c1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/ruby_lexer.rb
Expand Up @@ -58,6 +58,7 @@ class RubyLexer
"===" => :tEQQ,
"=>" => :tASSOC,
"=~" => :tMATCH,
"->" => :tLAMBDA,
}

# How the parser advances to the next token.
Expand Down Expand Up @@ -838,6 +839,12 @@ def yylex # 826 lines
return :tPIPE
end
elsif src.scan(/\{/) then
if defined?(@hack_expects_lambda) && @hack_expects_lambda
@hack_expects_lambda = false
self.lex_state = :expr_beg
return :tLAMBEG
end

result = if lex_state.is_argument || lex_state == :expr_end then
:tLCURLY # block (primary)
elsif lex_state == :expr_endarg then
Expand All @@ -850,6 +857,10 @@ def yylex # 826 lines
self.command_start = true unless result == :tLBRACE

return result
elsif src.scan(/->/) then
@hack_expects_lambda = true
self.lex_state = :expr_arg
return :tLAMBDA
elsif src.scan(/[+-]/) then
sign = src.matched
utype, type = if sign == "+" then
Expand Down Expand Up @@ -1262,6 +1273,10 @@ def process_token(command_state)
return :kDO_COND if cond.is_in_state
return :kDO_BLOCK if cmdarg.is_in_state && state != :expr_cmdarg
return :kDO_BLOCK if state == :expr_endarg
if defined?(@hack_expects_lambda) && @hack_expects_lambda
@hack_expects_lambda = false
return :kDO_LAMBDA
end
return :kDO
end

Expand Down
38 changes: 36 additions & 2 deletions lib/ruby_parser.y
Expand Up @@ -4,7 +4,7 @@ class RubyParser

token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
kTHEN kELSIF kELSE kCASE kWHEN kWHILE kUNTIL kFOR kBREAK kNEXT
kREDO kRETRY kIN kDO kDO_COND kDO_BLOCK kRETURN kYIELD kSUPER
kREDO kRETRY kIN kDO kDO_COND kDO_BLOCK kDO_LAMBDA kRETURN kYIELD kSUPER
kSELF kNIL kTRUE kFALSE kAND kOR kNOT kIF_MOD kUNLESS_MOD kWHILE_MOD
kUNTIL_MOD kRESCUE_MOD kALIAS kDEFINED klBEGIN klEND k__LINE__
k__FILE__ tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tNTH_REF
Expand All @@ -16,7 +16,7 @@ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
tTILDE tPERCENT tDIVIDE tPLUS tMINUS tLT tGT tPIPE tBANG tCARET
tLCURLY tRCURLY tBACK_REF2 tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG
tWORDS_BEG tAWORDS_BEG tSTRING_DBEG tSTRING_DVAR tSTRING_END tSTRING
tSYMBOL tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAST_TOKEN
tSYMBOL tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAST_TOKEN tLAMBDA tLAMBEG

prechigh
right tBANG tTILDE tUPLUS
Expand Down Expand Up @@ -997,6 +997,10 @@ rule
iter.insert 1, call
result = iter
}
| tLAMBDA lambda
{
result = val[1]
}
| kIF expr_value then compstmt if_tail kEND
{
result = new_if val[1], val[3], val[4]
Expand Down Expand Up @@ -1269,6 +1273,36 @@ rule
result = s(:zsuper)
}

lambda: lambda_body
{
call = s(:call, nil, :lambda, s(:arglist))
result = s(:iter, call, nil, val[0])
}
| f_arglist lambda_body
{
case val[0].size
when 1
args = 0
when 2
args = s(:lasgn, val[0][1])
else
vars = val[0][1..-1].map{|name| s(:lasgn, name)}
args = s(:masgn, s(:array, *vars))
end

call = s(:call, nil, :lambda, s(:arglist))
result = s(:iter, call, args, val[1])
}

lambda_body: tLAMBEG compstmt tRCURLY
{
result = val[1]
}
| kDO_LAMBDA compstmt kEND
{
result = val[1]
}

brace_block: tLCURLY
{
self.env.extend :dynamic
Expand Down

0 comments on commit 53d52c1

Please sign in to comment.