org.comedia.util.scanner
Class CPasScanner
java.lang.Object
|
+--org.comedia.util.scanner.CScanner
|
+--org.comedia.util.scanner.CPasScanner
- public class CPasScanner
- extends CScanner
Performs lexical scanning for Pascal-like languages.
Scanner supports all standard operators, keywords or datatypes of ANSI-Pascal.
Example of scanner usage:
System.out.println("*********** Pascal Scanner Test *************");
CPasScanner scanner = new CPasScanner();
scanner.setBuffer("while(1.0e2*i := \t\r\n> \'string\'\'\')\n"
+ "// comment\n(*second\ncomment*){xxx}");
scanner.setShowEol(true);
scanner.setShowSpace(true);
scanner.setShowComment(true);
scanner.setShowKeyword(true);
scanner.setShowType(true);
// Tests string convertions
String str = "The test \'string\'";
System.out.println("Start string: " + str);
str = scanner.wrapString(str);
System.out.println("Wrapped string: " + str);
str = scanner.unwrapString(str);
System.out.println("Unwrapped string: " + str);
System.out.println();
System.out.println("Initial string: " + scanner.getBuffer());
while (scanner.lex() != EOF) {
switch (scanner.getTokenType()) {
case UNKNOWN: System.out.print("Type: Unknown "); break;
case COMMENT: System.out.print("Type: Comment "); break;
case KEYWORD: System.out.print("Type: Keyword "); break;
case TYPE: System.out.print("Type: Type "); break;
case IDENT: System.out.print("Type: Ident "); break;
case ALPHA: System.out.print("Type: Alpha "); break;
case OPERATOR: System.out.print("Type: Operator "); break;
case BRACE: System.out.print("Type: Brace "); break;
case SEPARATOR: System.out.print("Type: Separator "); break;
case EOL: System.out.print("Type: Eol "); break;
case LF: System.out.print("Type: Lf "); break;
case SPACE: System.out.print("Type: Space "); break;
case INT: System.out.print("Type: Int "); break;
case FLOAT: System.out.print("Type: Float "); break;
case STRING: System.out.print("Type: String "); break;
case BOOL: System.out.print("Type: Bool "); break;
case EOF: System.out.print("Type: Eof "); break;
}
System.out.println("Value: '" + scanner.getToken()
+ "' Pos: " + scanner.getPosition() + " Line: " + scanner.getLineNo());
}
The result:
*********** Pascal Scanner Test *************
Start string: The test 'string'
Wrapped string: 'The test ''string'''
Unwrapped string: The test 'string'
Initial string: while(1.0e2*i :=
> 'string''')
// comment
(*second
comment*){xxx}
Type: Keyword Value: 'while' Pos: 0 Line: 0
Type: Brace Value: '(' Pos: 5 Line: 0
Type: Float Value: '1.0e2' Pos: 6 Line: 0
Type: Operator Value: '*' Pos: 11 Line: 0
Type: Ident Value: 'i' Pos: 12 Line: 0
Type: Space Value: ' ' Pos: 13 Line: 0
Type: Operator Value: ':=' Pos: 14 Line: 0
Type: Space Value: ' ' Pos: 16 Line: 0
Type: Lf Value: '
' Pos: 18 Line: 0
Type: Eol Value: '
' Pos: 19 Line: 0
Type: Operator Value: '>' Pos: 20 Line: 1
Type: Space Value: ' ' Pos: 21 Line: 1
Type: String Value: ''string'''' Pos: 22 Line: 1
Type: Brace Value: ')' Pos: 32 Line: 1
Type: Eol Value: '
' Pos: 33 Line: 1
Type: Comment Value: '// comment
' Pos: 34 Line: 2
Type: Comment Value: '(*second
comment*)' Pos: 45 Line: 3
Type: Comment Value: '{xxx}' Pos: 63 Line: 4
Field Summary |
protected java.lang.String[] |
pasKeywords
|
protected java.lang.String[] |
pasOperators
List of Pascal specified operators. |
protected java.lang.String[] |
pasTypes
List of Pascal specified data type keywords. |
Fields inherited from class org.comedia.util.scanner.CScanner |
ALPHA, BOOL, BRACE, buffer, bufferLen, bufferLine, bufferPos, COMMENT, CONST, current, DELIM, EOF, EOL, FLOAT, IDENT, INT, KEYWORD, keywords, LF, next, OPERATOR, operators, SEPARATOR, showComment, showEol, showKeyword, showSpace, showString, showType, SPACE, STRING, TYPE, types, UNKNOWN |
Constructor Summary |
CPasScanner()
Default class constructor. |
Method Summary |
protected int |
lowRunLex(CScanner.Lexem curr)
Gets a lowlevel token. |
static void |
main(java.lang.String[] args)
The main function for test purposes. |
static java.lang.String |
unwrapString(java.lang.String s)
Converts a string from Pascal-like escape format limited
with quotes into oridinary (local) presentation. |
static java.lang.String |
wrapString(java.lang.String s)
Converts a string from ordinary into Pascal-like escape format
limited with quotes. |
Methods inherited from class org.comedia.util.scanner.CScanner |
extractNextToken, extractToken, getBuffer, getBufferPos, getLineNo, getNextLineNo, getNextPosition, getNextToken, getNextTokenType, getPosition, getToken, getTokenType, gotoNextToken, innerProcCComment, innerProcCString, innerProcIdent, innerProcLineComment, innerProcPasString, innerProcString, innerStartLex, isAlpha, isDelim, isDigit, isEol, isLetter, isQuote, isShowComment, isShowEol, isShowKeyword, isShowSpace, isShowString, isShowType, isWhite, lex, restart, runLex, searchForString, setBuffer, setShowComment, setShowEol, setShowKeyword, setShowSpace, setShowString, setShowType |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
pasOperators
protected java.lang.String[] pasOperators
- List of Pascal specified operators.
pasTypes
protected java.lang.String[] pasTypes
- List of Pascal specified data type keywords.
pasKeywords
protected java.lang.String[] pasKeywords
CPasScanner
public CPasScanner()
- Default class constructor.
lowRunLex
protected int lowRunLex(CScanner.Lexem curr)
- Gets a lowlevel token. Presents the main parsing process.
- Overrides:
lowRunLex
in class CScanner
- Parameters:
curr
- a "Holder" which containes extracted token.
wrapString
public static java.lang.String wrapString(java.lang.String s)
- Converts a string from ordinary into Pascal-like escape format
limited with quotes.
- Parameters:
s
- a string in ordinary (local) presentation.
unwrapString
public static java.lang.String unwrapString(java.lang.String s)
- Converts a string from Pascal-like escape format limited
with quotes into oridinary (local) presentation.
- Parameters:
s
- a string in Pascal-like escape format.
main
public static void main(java.lang.String[] args)
- The main function for test purposes.