Skip to content

Commit

Permalink
Block comment support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Brown committed Oct 9, 2010
1 parent 427d3e0 commit c74f46f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions jmeta/SyntaxError.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ private static String makeMsg(String msg, int pos, String string, Object[] list)
else msg = "expected "+ BaseParser.ERROR.last;

String s = string.substring(pos, Math.min(pos + 13, string.length()));
if (pos >= string.length()) {
s = "<EOF>";
}
int n = s.indexOf('\n');
if (n > 0) s = s.substring(0, n);
msg = msg + " before '"+ s +"'";
Expand Down
3 changes: 2 additions & 1 deletion test/Mirah.mmeta
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ parser MirahParser {
sp: oneOf(" \t\f\r\v") | "\\" nl | comment;
@Memo
ws: (oneOf(" \t\f\r\v") | "\\" nl | comment)*;
comment: "#" (~"\n" _)*;
comment: "#" (~"\n" _)* | block_comment;
block_comment: "/*"! (block_comment | (~"*/" _))* "*/";
start: ! prog=compstmt $EOF heredoc -> ^(Script prog);
compstmt scope(@cond)
: UNCOND
Expand Down
29 changes: 29 additions & 0 deletions test/MirahLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ private int skipWhitespace(int pos) {
i = end;
}
break ws;
case '/':
if (i + 1 < end && chars[i + 1] == '*') {
i = skipBlockComment(i + 2);
break;
}
break ws;
default:
break ws;
}
Expand All @@ -47,6 +53,29 @@ private int skipWhitespace(int pos) {
return i;
}

private int skipBlockComment(int start) {
int i = start;
while (i < end) {
switch(chars[i]) {
case '\n':
parser.note_newline(i);
break;
case '*':
if (i + 1 < end && chars[i + 1] == '/') {
return i + 1;
}
break;
case '/':
if (i + 1 < end && chars[i + 1] == '*') {
i = skipBlockComment(i + 2);
}
break;
}
i += 1;
}
throw new jmeta.SyntaxError("*/", end, string, null);
}

private Tokens processFirstChar(int i) {
Tokens type = null;
if (i == end) {
Expand Down
4 changes: 4 additions & 0 deletions test/test_mirah.rb
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,8 @@ def test_return
def test_call_assocs
assert_parse("[Script, [FCall, puts, [[Hash, [Assoc, [Symbol, a], [Symbol, b]]]], null]]", "puts :a => :b")
end

def test_block_comment
assert_parse("[Script, [Fixnum, 3]]", "/* A /* nested */ comment */3")
end
end

0 comments on commit c74f46f

Please sign in to comment.