org.comedia.util.scanner
Class CCppScanner

java.lang.Object
  |
  +--org.comedia.util.scanner.CScanner
        |
        +--org.comedia.util.scanner.CCppScanner
Direct Known Subclasses:
CJavaScanner

public class CCppScanner
extends CScanner

Performs lexical scanning for C/C++ languages. Scanner supports all standard operators, keywords or datatypes of C/C++.

Example of scanner usage:

 System.out.println("*********** C Scanner Test *************");

 CCppScanner scanner = new CCppScanner();
 scanner.setBuffer("while(1.0e2=i.a >>= \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:

 *********** C Scanner Test *************
 Start string: The test "string"
 Wrapped string: "The test \"string\""
 Unwrapped string: The test "string"

 Initial string: while(1.0e2=i.a >>=
 -> "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: Separator Value: '.' Pos: 13 Line: 0
 Type: Ident Value: 'a' Pos: 14 Line: 0
 Type: Space Value: ' ' Pos: 15 Line: 0
 Type: Operator Value: '>>=' Pos: 16 Line: 0
 Type: Space Value: ' 	' Pos: 19 Line: 0
 Type: Lf Value: '
 ' Pos: 21 Line: 0
 Type: Eol Value: '
 ' Pos: 22 Line: 0
 Type: Operator Value: '->' Pos: 23 Line: 1
 Type: Space Value: ' ' Pos: 25 Line: 1
 Type: String Value: '"string\""' Pos: 26 Line: 1
 Type: Brace Value: ')' Pos: 36 Line: 1
 Type: Eol Value: '
 ' Pos: 37 Line: 1
 Type: Comment Value: '// comment
 ' Pos: 38 Line: 2
 Type: Comment Value: '/*second
 comment*./' Pos: 49 Line: 3
 Type: Brace Value: '{' Pos: 67 Line: 4
 Type: Ident Value: 'xxx' Pos: 68 Line: 4
 Type: Brace Value: '}' Pos: 71 Line: 4
 


Inner classes inherited from class org.comedia.util.scanner.CScanner
CScanner.Lexem
 
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
CCppScanner()
          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 C-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 C-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
 

Constructor Detail

CCppScanner

public CCppScanner()
Default class constructor.
Method Detail

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 C-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 C-like escape format limited with quotes into oridinary (local) presentation.
Parameters:
s - a string in C-like escape format.

main

public static void main(java.lang.String[] args)
The main function for test purposes.