com.google.caja.lexer
Class FilePosition

java.lang.Object
  extended by com.google.caja.lexer.FilePosition
All Implemented Interfaces:
MessagePart

public final class FilePosition
extends java.lang.Object
implements MessagePart

The range of characters in a source file occupied by a token or a group of tokens.

Fields are 1 indexed since that is how most text editors display line and character (UTF-16 code-unit) positions. The start numbers are inclusive, and the end numbers are exclusive, so the number of characters in a token is (startCharInFile - endCharInFile).

Line numbers are 1 greater than the number of line breaks where a line-break is defined greedily as one of {{{CR}}}, {{{LF}}}, {{{CRLF}}}. Bash, javascript, or python style line-continuations like

   a, b, c = 0, 1, \
       2
 
are treated as line breaks. Line numbers are determined solely based on the characters in the file and do not represent a logical unit of a program.

File Positions are independent of the language being parsed.

Author:
mikesamuel@gmail.com

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.caja.reporting.MessagePart
MessagePart.Factory
 
Field Summary
private  SourceBreaks breaks
           
private  int length
           
private  int startCharInFile
           
static FilePosition UNKNOWN
           
 
Constructor Summary
FilePosition(SourceBreaks breaks, int startCharInFile, int length)
           
 
Method Summary
static FilePosition between(FilePosition a, FilePosition b)
           
 int endCharInFile()
           
 int endCharInLine()
           
 int endLineNo()
           
static FilePosition endOf(FilePosition fp)
           
static FilePosition endOfOrNull(FilePosition fp)
           
 boolean equals(java.lang.Object o)
           
 void format(MessageContext context, java.lang.Appendable out)
          Formats this part to out.
 void formatShort(java.lang.Appendable out)
           
static FilePosition fromLinePositions(InputSource source, int startLineNo, int startCharInLine, int endLineNo, int endCharInLine)
          Produce a FilePosition divorced from SourceBreaks to ease testing.
(package private)  SourceBreaks getBreaks()
           
 int hashCode()
           
static FilePosition instance(InputSource source, int lineNo, int charInFile, int charInLine)
           
static FilePosition instance(InputSource source, int lineNo, int charInFile, int charInLine, int length)
           
 int length()
           
 InputSource source()
           
static FilePosition span(FilePosition start, FilePosition end)
           
 int startCharInFile()
          1 greater than the number of characters since the beginning of the file.
 int startCharInLine()
          1 greater than the number of characters since the last newline character.
 int startLineNo()
          1 greater than the number of newlines between the start of the token and the beginning of the file.
static FilePosition startOf(FilePosition fp)
           
static FilePosition startOfFile(InputSource source)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

UNKNOWN

public static final FilePosition UNKNOWN

breaks

private final SourceBreaks breaks

startCharInFile

private final int startCharInFile

length

private final int length
Constructor Detail

FilePosition

FilePosition(SourceBreaks breaks,
             int startCharInFile,
             int length)
Method Detail

getBreaks

SourceBreaks getBreaks()

source

public InputSource source()

startLineNo

public int startLineNo()
1 greater than the number of newlines between the start of the token and the beginning of the file.


startCharInFile

public int startCharInFile()
1 greater than the number of characters since the beginning of the file.


startCharInLine

public int startCharInLine()
1 greater than the number of characters since the last newline character.


endLineNo

public int endLineNo()

endCharInFile

public int endCharInFile()

endCharInLine

public int endCharInLine()

length

public int length()

between

public static FilePosition between(FilePosition a,
                                   FilePosition b)

instance

public static FilePosition instance(InputSource source,
                                    int lineNo,
                                    int charInFile,
                                    int charInLine)

instance

public static FilePosition instance(InputSource source,
                                    int lineNo,
                                    int charInFile,
                                    int charInLine,
                                    int length)

fromLinePositions

public static FilePosition fromLinePositions(InputSource source,
                                             int startLineNo,
                                             int startCharInLine,
                                             int endLineNo,
                                             int endCharInLine)
Produce a FilePosition divorced from SourceBreaks to ease testing.


startOfFile

public static FilePosition startOfFile(InputSource source)

span

public static FilePosition span(FilePosition start,
                                FilePosition end)

startOf

public static FilePosition startOf(FilePosition fp)

endOf

public static FilePosition endOf(FilePosition fp)

endOfOrNull

public static FilePosition endOfOrNull(FilePosition fp)

format

public final void format(MessageContext context,
                         java.lang.Appendable out)
                  throws java.io.IOException
Description copied from interface: MessagePart
Formats this part to out.

Specified by:
format in interface MessagePart
out - receives the formatted form of this.
Throws:
java.io.IOException

formatShort

public final void formatShort(java.lang.Appendable out)
                       throws java.io.IOException
Throws:
java.io.IOException

toString

public final java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public final boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public final int hashCode()
Overrides:
hashCode in class java.lang.Object


Copyright (C) 2008 Google Inc.
Licensed under the Apache License, Version 2.0