001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.dev.tool.localeconverter;
009:
010: import java.io.*;
011:
012: /**
013: * This transition parses an end-of-line sequence. The comment character
014: * can be set to an arbitrary character, but it is shared globally.
015: * A comment may only occur after an end-of-line.
016: * EOL := <EOF> | <EOL_CHARS> [ <EOL_SEGMENT> ]*
017: * EOL_SEGMENT := <EOL_CHARS> | <SPACE_CHARS> | <COMMENT>
018: * SPACE_CHARS := " \t";
019: * EOL_CHARS = "\r\n\u2028\u2029";
020: * COMMENT_STRING := <COMMENT_CHAR> <COMMENT_BODY>
021: * COMMENT_CHAR = "#";
022: * COMMENT_BODY = [ ~<EOF> & ~<EOL_CHARS> ]*
023: */
024: public class EOLTransition extends ComplexTransition {
025: public static final String EOL_CHARS = "\f\r\n\u2028\u2029";
026: public static final char DEFAULT_COMMENT_CHAR = '#';
027: public static char COMMENT_CHAR = DEFAULT_COMMENT_CHAR;
028: public static final EOLTransition GLOBAL = new EOLTransition(
029: SUCCESS);
030:
031: /** Restore the comment character to the default value */
032: public static synchronized char setDefaultCommentChar() {
033: return setCommentChar(DEFAULT_COMMENT_CHAR);
034: }
035:
036: /** Set a new comment character */
037: public static synchronized char setCommentChar(char c) {
038: char result = COMMENT_CHAR;
039: COMMENT_CHAR = c;
040: states = null; //flush states
041: return result;
042: }
043:
044: public EOLTransition(int success) {
045: super (success);
046: //{{INIT_CONTROLS
047: //}}
048: }
049:
050: public boolean accepts(int c) {
051: return (EOL_CHARS.indexOf((char) c) >= 0 || COMMENT_CHAR == c);
052: }
053:
054: protected Lex.Transition[][] getStates() {
055: synchronized (getClass()) {
056: if (states == null) {
057: //cache the states so they can be shared. This states
058: //need to be flushed and rebuilt when the comment
059: //character changes.
060: states = new Lex.Transition[][] {
061: { //state 0:
062: new Lex.StringTransition(EOL_CHARS,
063: Lex.IGNORE_CONSUME, -1),
064: new Lex.EOFTransition(SUCCESS), },
065: { //state 1:
066: new Lex.CharTransition(COMMENT_CHAR,
067: Lex.IGNORE_CONSUME, -2),
068: new Lex.StringTransition(EOL_CHARS,
069: Lex.IGNORE_CONSUME, -1),
070: new Lex.StringTransition(
071: SpaceTransition.SPACE_CHARS,
072: Lex.IGNORE_CONSUME, -1),
073: new Lex.EOFTransition(SUCCESS),
074: new Lex.DefaultTransition(
075: Lex.IGNORE_PUTBACK, SUCCESS) },
076: { //state 2:
077: new Lex.StringTransition(EOL_CHARS,
078: Lex.IGNORE_CONSUME, -1),
079: new Lex.EOFTransition(SUCCESS),
080: new Lex.DefaultTransition(
081: Lex.IGNORE_CONSUME, -2) } };
082: }
083: }
084: return states;
085: }
086:
087: private static Lex.Transition[][] states;
088:
089: public static void main(String args[]) {
090: try {
091:
092: final int TOKEN = 1;
093: final int EOF = 2;
094: final int EOL = 3;
095: final int RANGE = 4;
096: Lex.Transition[][] states = { {
097: new Lex.EOFTransition(EOF),
098: new EOLTransition(EOL),
099: new SymbolTransition(TOKEN),
100: new SpaceTransition(0),
101: new RangeTransition(RANGE),
102: new EOLTransition(EOL),
103: new Lex.DefaultTransition(Lex.ACCUMULATE_CONSUME, 0) } };
104: String text = "\n<NULL> 0x1243 #hello kdsj\n";
105: StringReader sr = new StringReader(text);
106: PushbackReader pr = new PushbackReader(sr);
107: Lex parser = new Lex(states, pr);
108: parser.debug(true);
109: parser.debug(true);
110: int s = parser.nextToken();
111: while (s == EOL) {
112: System.out.println(parser.getData());
113: s = parser.nextToken();
114: while (s == TOKEN) {
115: System.out.println(parser.getData());
116: s = parser.nextToken();
117: }
118: }
119: } catch (Exception e) {
120: System.out.println(e);
121: }
122: }
123: //{{DECLARE_CONTROLS
124: //}}
125: }
|