Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Wednesday, November 18, 2009

Playing with JRuby's Parser

Apply this patch to JRuby.

diff --git a/src/org/jruby/lexer/yacc/RubyYaccLexer.java b/src/org/jruby/lexer/yacc/RubyYaccLexer.java
index dd1733e..dc591a2 100644
--- a/src/org/jruby/lexer/yacc/RubyYaccLexer.java
+++ b/src/org/jruby/lexer/yacc/RubyYaccLexer.java
@@ -1208,6 +1208,12 @@ public class RubyYaccLexer {
     private int colon(boolean spaceSeen) throws IOException {
         int c = src.read();

+        if (c == '-' && src.peek(')')) {
+          src.read();
+          lex_state = LexState.EXPR_BEG;
+          yaccValue = new Token("=>", getPosition());
+          return Tokens.tASSOC;
+        }
         if (c == ':') {
             if (isBEG() || lex_state == LexState.EXPR_CLASS || (isARG() && spaceSeen)) {
                 lex_state = LexState.EXPR_BEG;

Now you can write :-) as => in braces as Hash literals.

$ ./bin/jruby ./bin/jirb
> {1 => 2}
=> {1=>2}
> {1 :-) 2}
=> {1=>2}
> {:aaa => :bbb}
=> {:aaa=>:bbb}
> {:aaa:-):bbb}
=> {:aaa=>:bbb}

This patch is almost same as my previous patch to MRI.

diff --git a/parse.y b/parse.y
index b8d9b59..a592aed 100644
--- a/parse.y
+++ b/parse.y
@@ -7125,6 +7125,11 @@ parser_yylex(struct parser_params *parser)

       case ':':
         c = nextc();
+        if (c == '-' && peek(')') {
+            nextc();
+            lex_state = EXPR_BEG;
+            return tASSOC;
+        }
         if (c == ':') {
             if (IS_BEG() ||
                 lex_state == EXPR_CLASS || (IS_ARG() && space_seen)) {

No comments:

Post a Comment

Followers