public void skipMultiLineComment() { int start = 0; Loc loc = new Loc(); char ch; string comment; if (extra.comments.Any()) { start = index - 2; loc = new Loc() { start = new Loc.Position() { line = lineNumber, column = index - lineStart - 2 } }; } while (index < length) { ch = source.ToCharArray()[index]; if (isLineTerminator(ch)) { if (ch == 0x0D && source.ToCharArray()[index + 1] == 0x0A) { ++index; } hasLineTerminator = true; ++lineNumber; ++index; lineStart = index; } else if (ch == 0x2A) { // Block comment ends with "*/". if (source.ToCharArray()[index + 1] == 0x2F) { ++index; ++index; if (extra.comments.Any()) { comment = source.Substring(start + 2, index - 2); loc.end = new Loc.Position() { line = lineNumber, column = index - lineStart }; addComment("Block", comment, start, index, loc); } return; } ++index; } else { ++index; } } // Ran off the end of the file - the whole thing is a comment if (extra.comments.Any()) { loc.end = new Loc.Position() { line = lineNumber, column = index - lineStart }; comment = source.Substring(start + 2, index); addComment("Block", comment, start, index, loc); } tolerateUnexpectedToken(); }
public void skipSingleLineComment(int offset) { int start; Loc loc; char ch; string comment; start = index - offset; loc = new Loc() { start = new Loc.Position() { line = lineNumber, column = index - lineStart - offset } }; while (index < length) { ch = source.ToCharArray()[index]; ++index; if (isLineTerminator(ch)) { hasLineTerminator = true; if (extra.comments.Any()) { comment = source.Substring(start + offset, index - 1); loc.end = new Loc.Position() { line = lineNumber, column = index - lineStart - 1 }; addComment("Line", comment, start, index - 1, loc); } if (ch == 13 && source.ToCharArray()[index] == 10) { ++index; } ++lineNumber; lineStart = index; return; } } if (extra.comments.Any()) { comment = source.Substring(start + offset, index); loc.end = new Loc.Position() { line = lineNumber, column = index - lineStart }; addComment("Line", comment, start, index, loc); } }
public Token collectToken() { Loc loc; Token token; string value; Token entry; loc = new Loc() { start = new Loc.Position() { line = lineNumber, column = index - lineStart } }; token = advance(); loc.end = new Loc.Position() { line = lineNumber, column = index - lineStart }; if (token.type != TokenType.EOF) { value = source.Substring(token.start, token.end - token.start); entry = new Token { type = token.type, value = value, range = new Token.TokenRange() { start = token.start, end = token.end }, loc = loc, lineNumber = lineNumber, start = loc.start.column, end = loc.end.column, }; if (token.regex != null) { entry.regex = new Regex() { pattern = token.regex.pattern, flags = token.regex.flags }; } extra.tokens.Add(entry); } return token; }
// ECMA-262 11.4 Comments public void addComment(string type, string value, int start, int end, Loc loc) { Comment comment; // assert(typeof start == "number", "Comment must have valid position"); state.lastCommentStart = start; comment = new Comment() { type = type, value = value }; if (extra.range != null) { comment.range = new Range() { Start = start, End = end }; } if (extra.loc != null) { comment.loc = loc; } extra.comments.Add(comment); if (extra.attachComment) { extra.leadingComments.Add(comment); extra.trailingComments.Add(comment); } }
public Token collectRegex() { int pos; Loc loc; Token regex; Token token; skipComment(); pos = index; loc = new Loc() { start = new Loc.Position() { line = lineNumber, column = index - lineStart } }; regex = scanRegExp(); loc.end = new Loc.Position() { line = lineNumber, column = index - lineStart }; /* istanbul ignore next */ if (!extra.tokenize) { // Pop the previous token, which is likely "/" or "/=" if (extra.tokens.Count > 0) { token = extra.tokens[extra.tokens.Count - 1]; // if (token.range] == pos && token.type == TokenType.Punctuator) { if (token.value == "/" || token.value == "/=") { extra.tokens.Remove(extra.tokens.Last()); } } } extra.tokens.Add(new Token() { type = TokenType.RegularExpression, value = regex.literal, regex = regex.regex, range = new Token.TokenRange() { start = pos, end = index }, loc = loc }); } return regex; }