Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser support for legacy JS comments: <!-- and --> #88

Open
dmitris opened this issue Jul 8, 2014 · 1 comment
Open

parser support for legacy JS comments: <!-- and --> #88

dmitris opened this issue Jul 8, 2014 · 1 comment

Comments

@dmitris
Copy link
Contributor

dmitris commented Jul 8, 2014

The program below produces an error:

2014/07/08 21:56:58 Error parsing JS: (anonymous): Line 2:5 Unexpected token <
exit status 1

Would it be possible to expand the parser to cover those? It would help for example for tools using otto parser for analysis of Javascript from the websites. Node, v8 and esprima handle those strings as line comments - similar to // .

References:
http://www.javascripter.net/faq/comments.htm
http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html:
for related historical reasons, the string "<!--" in JavaScript is actually treated as a line comment start, just like "//".

package main

import (
    "log"

    "github.com/robertkrimen/otto/parser"
)

func main() {
    filename := "" // A filename is optional
    src := `xyz = 49.1;
    <!-- Sample xyzzy example 
    <!-- --> http://www.javascripter.net/faq/comments.htm
`
    // Parse some JavaScript, yielding a *ast.Program and/or an ErrorList
    _, err := parser.ParseFile(nil, filename, src, 0)
    if err != nil {
        log.Fatalf("Error parsing JS: %s\n", err)
    }
}
@willglynn
Copy link

v8's scanner has some implementation notes:

Token::Value Scanner::ScanHtmlComment() {
  // Check for <!-- comments.
  ASSERT(c0_ == '!');
  Advance();
  if (c0_ == '-') {
    Advance();
    if (c0_ == '-') return SkipSingleLineComment();
    PushBack('-');  // undo Advance()
  }
  PushBack('!');  // undo Advance()
  ASSERT(c0_ == '!');
  return Token::LT;
}

//

// If there is an HTML comment end '-->' at the beginning of a
// line (with only whitespace in front of it), we treat the rest
// of the line as a comment. This is in line with the way
// SpiderMonkey handles it.
if (c0_ == '-' && has_line_terminator_before_next_) {
  Advance();
  if (c0_ == '-') {
    Advance();
    if (c0_ == '>') {
      // Treat the rest of the line as a comment.
      SkipSingleLineComment();
      // Continue skipping white space after the comment.
      continue;
    }
    PushBack('-');  // undo Advance()
  }
  PushBack('-');  // undo Advance()
}

//

case '-':
  // - -- --> -=
  Advance();
  if (c0_ == '-') {
    Advance();
    if (c0_ == '>' && has_line_terminator_before_next_) {
      // For compatibility with SpiderMonkey, we skip lines that
      // start with an HTML comment end '-->'.
      token = SkipSingleLineComment();
    } else {
      token = Token::DEC;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants