public SourceFile(String name, String contents) { this.name = name; this.contents = contents; this.lineNumberTable = new LineNumberTable(this); } }
public LineNumberTable(SourceFile sourceFile) { this.sourceFile = sourceFile; this.lineStartOffsets = computeLineStartOffsets(sourceFile.contents); }
private static int[] computeLineStartOffsets(String source) { // TODO(johnlenz): do this more efficiently. ArrayList<Integer> lineStartOffsets = new ArrayList<>(); lineStartOffsets.add(0); for (int index = 0; index < source.length(); index++) { char ch = source.charAt(index); if (isLineTerminator(ch)) { if (index + 1 < source.length() && ch == '\r' && source.charAt(index + 1) == '\n') { index++; } lineStartOffsets.add(index + 1); } } lineStartOffsets.add(Integer.MAX_VALUE); return toIntArray(lineStartOffsets); }
private SourceRange getTokenRange(int startOffset) { return getLineNumberTable().getSourceRange(startOffset, index); }
public SourceRange getSourceRange(int startOffset, int endOffset) { return new SourceRange(getSourcePosition(startOffset), getSourcePosition(endOffset)); } }
private int getColumn(int line, int offset) { return offset - offsetOfLine(line); }
private static int[] computeLineStartOffsets(String source) { // TODO(johnlenz): do this more efficiently. ArrayList<Integer> lineStartOffsets = new ArrayList<Integer>(); lineStartOffsets.add(0); for (int index = 0; index < source.length(); index++) { char ch = source.charAt(index); if (isLineTerminator(ch)) { if (index + 1 < source.length() && ch == '\r' && source.charAt(index + 1) == '\n') { index++; } lineStartOffsets.add(index + 1); } } lineStartOffsets.add(Integer.MAX_VALUE); return toIntArray(lineStartOffsets); }
private SourceRange getTokenRange(int startOffset) { return getLineNumberTable().getSourceRange(startOffset, index); }
public SourceRange getSourceRange(int startOffset, int endOffset) { return new SourceRange(getSourcePosition(startOffset), getSourcePosition(endOffset)); } }
private int getColumn(int line, int offset) { return offset - offsetOfLine(line); }
private void skipSingleLineComment(Comment.Type type) { int startOffset = index; while (!isAtEnd() && !isLineTerminator(peekChar())) { nextChar(); } SourceRange range = getLineNumberTable().getSourceRange(startOffset, index); String value = this.contents.substring(startOffset, index); recordComment(type, range, value); }
private SourcePosition getPosition(int offset) { return getLineNumberTable().getSourcePosition(offset); }
public SourceFile(String name, String contents) { this.name = name; this.contents = contents; this.lineNumberTable = new LineNumberTable(this); } }
public LineNumberTable(SourceFile sourceFile) { this.sourceFile = sourceFile; this.lineStartOffsets = computeLineStartOffsets(sourceFile.contents); }
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(type, range, value); }
private SourcePosition getPosition(int offset) { return getLineNumberTable().getSourcePosition(offset); }
private void skipMultiLineComment() { int startOffset = index; nextChar(); // '/' nextChar(); // '*' while (!isAtEnd() && (peekChar() != '*' || peekChar(1) != '/')) { nextChar(); } if (!isAtEnd()) { nextChar(); nextChar(); Comment.Type type = Comment.Type.BLOCK; if (index - startOffset > 4) { if (this.contents.charAt(startOffset + 2) == '*') { type = Comment.Type.JSDOC; } else if (this.contents.charAt(startOffset + 2) == '!') { type = Comment.Type.IMPORTANT; } } SourceRange range = getLineNumberTable().getSourceRange(startOffset, index); String value = this.contents.substring(startOffset, index); recordComment(type, range, value); } else { reportError("unterminated comment"); } }
private void skipMultiLineComment() { int startOffset = index; nextChar(); // '/' nextChar(); // '*' while (!isAtEnd() && (peekChar() != '*' || peekChar(1) != '/')) { nextChar(); } if (!isAtEnd()) { nextChar(); nextChar(); Comment.Type type = (index - startOffset > 4 && this.source.contents.charAt(startOffset + 2) == '*') ? Comment.Type.JSDOC : Comment.Type.BLOCK; SourceRange range = getLineNumberTable().getSourceRange( startOffset, index); String value = this.source.contents.substring( startOffset, index); recordComment(type, range, value); } else { reportError("unterminated comment"); } }