Skip to content

Commit

Permalink
Add shebang comments to the new parser. Fixes #410
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks committed May 13, 2014
1 parent 8a895fd commit 74b71e5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/com/google/javascript/jscomp/parsing/parser/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ private boolean skipComment() {
return true;
}
break;
case '#':
if (index == 0 && peekChar(1) == '!') {
skipSingleLineComment(Comment.Type.SHEBANG);
return true;
}
break;
}
}
return false;
Expand All @@ -329,13 +335,17 @@ private void reportHtmlCommentWarning() {
}

private void skipSingleLineComment() {
skipSingleLineComment(Comment.Type.LINE);
}

private void skipSingleLineComment(Comment.Type type) {
int startOffset = index;
while (!isAtEnd() && !isLineTerminator(peekChar())) {
nextChar();
}
SourceRange range = getLineNumberTable().getSourceRange(startOffset, index);
String value = this.source.contents.substring(startOffset, index);
recordComment(Comment.Type.LINE, range, value);
recordComment(type, range, value);
}

private void recordComment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@
/** placeholder class */
public class Comment {
public static enum Type {
// /* comment */
BLOCK,

// // comment
LINE,
JSDOC

// /** comment */
JSDOC,

// #!/usr/bin/node
// Only valid at the start of a file.
SHEBANG
}

public final String value;
Expand Down
6 changes: 6 additions & 0 deletions test/com/google/javascript/jscomp/parsing/NewParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,12 @@ public void testForOf2() {
"for-of statement may not have initializer");
}

public void testShebang() {
parse("#!/usr/bin/node\n var x = 1;");
parseError("var x = 1; \n #!/usr/bin/node",
"primary expression expected");
}

private Node script(Node stmt) {
Node n = new Node(Token.SCRIPT, stmt);
n.setIsSyntheticBlock(true);
Expand Down

0 comments on commit 74b71e5

Please sign in to comment.