From feccee87a78e68d575dbdf44b34ca0cb5a21ea8d Mon Sep 17 00:00:00 2001 From: lhauch Date: Thu, 5 Oct 2006 23:12:07 +0000 Subject: Restructuring for better separation of Tool packages. git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1674 6f19259b-4bc3-4df7-8a09-765794883524 --- .../antcontrib/cpptasks/parser/AbstractParser.java | 67 +++++++++++++ .../cpptasks/parser/AbstractParserState.java | 41 ++++++++ .../sf/antcontrib/cpptasks/parser/BranchState.java | 46 +++++++++ .../net/sf/antcontrib/cpptasks/parser/CParser.java | 78 +++++++++++++++ .../parser/CaseInsensitiveLetterState.java | 87 +++++++++++++++++ .../antcontrib/cpptasks/parser/FilenameState.java | 41 ++++++++ .../antcontrib/cpptasks/parser/FortranParser.java | 106 +++++++++++++++++++++ .../sf/antcontrib/cpptasks/parser/LetterState.java | 80 ++++++++++++++++ .../net/sf/antcontrib/cpptasks/parser/Parser.java | 28 ++++++ .../net/sf/antcontrib/cpptasks/parser/PostE.java | 41 ++++++++ .../WhitespaceOrCaseInsensitiveLetterState.java | 83 ++++++++++++++++ .../cpptasks/parser/WhitespaceOrLetterState.java | 75 +++++++++++++++ 12 files changed, 773 insertions(+) create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/AbstractParser.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/AbstractParserState.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/BranchState.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/CParser.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/CaseInsensitiveLetterState.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/FilenameState.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/FortranParser.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/LetterState.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/Parser.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/PostE.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/WhitespaceOrCaseInsensitiveLetterState.java create mode 100644 Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/WhitespaceOrLetterState.java (limited to 'Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser') diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/AbstractParser.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/AbstractParser.java new file mode 100644 index 0000000000..6f0b79c072 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/AbstractParser.java @@ -0,0 +1,67 @@ +/* + * + * Copyright 2001-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; +import java.io.IOException; +import java.io.Reader; +/** + * An abstract base class for simple parsers + * + * @author Curt Arnold + */ +public abstract class AbstractParser { + /** + * + * + */ + protected AbstractParser() { + } + protected abstract void addFilename(String filename); + public abstract AbstractParserState getNewLineState(); + protected void parse(Reader reader) throws IOException { + char[] buf = new char[4096]; + AbstractParserState newLineState = getNewLineState(); + AbstractParserState state = newLineState; + int charsRead = -1; + do { + charsRead = reader.read(buf, 0, buf.length); + if (state == null) { + for (int i = 0; i < charsRead; i++) { + if (buf[i] == '\n') { + state = newLineState; + break; + } + } + } + if (state != null) { + for (int i = 0; i < charsRead; i++) { + state = state.consume(buf[i]); + // + // didn't match a production, skip to a new line + // + if (state == null) { + for (; i < charsRead; i++) { + if (buf[i] == '\n') { + state = newLineState; + break; + } + } + } + } + } + } while (charsRead >= 0); + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/AbstractParserState.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/AbstractParserState.java new file mode 100644 index 0000000000..fe66dbbef0 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/AbstractParserState.java @@ -0,0 +1,41 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; +/** + * An base class for objects that represent the state of an AbstractParser. + * + * @author CurtArnold + * @see AbstractParser + */ +public abstract class AbstractParserState { + private AbstractParser parser; + protected AbstractParserState(AbstractParser parser) { + if (parser == null) { + throw new NullPointerException("parser"); + } + this.parser = parser; + } + /** + * Consume a character + * + * @return new state, may be null to ignore the rest of the line + */ + public abstract AbstractParserState consume(char ch); + protected AbstractParser getParser() { + return parser; + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/BranchState.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/BranchState.java new file mode 100644 index 0000000000..370c7d5836 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/BranchState.java @@ -0,0 +1,46 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; +public class BranchState extends AbstractParserState { + private char[] branchChars; + private AbstractParserState[] branchStates; + private AbstractParserState noMatchState; + public BranchState(AbstractParser parser, char[] branchChars, + AbstractParserState[] branchStates, AbstractParserState noMatchState) { + super(parser); + this.branchChars = (char[]) branchChars.clone(); + this.branchStates = (AbstractParserState[]) branchStates.clone(); + this.noMatchState = noMatchState; + } + public AbstractParserState consume(char ch) { + AbstractParserState state; + for (int i = 0; i < branchChars.length; i++) { + if (ch == branchChars[i]) { + state = branchStates[i]; + return state.consume(ch); + } + } + state = getNoMatchState(); + if (state != null) { + return state.consume(ch); + } + return state; + } + protected AbstractParserState getNoMatchState() { + return noMatchState; + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/CParser.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/CParser.java new file mode 100644 index 0000000000..07f8eba4e2 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/CParser.java @@ -0,0 +1,78 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; +import java.io.IOException; +import java.io.Reader; +import java.util.Vector; +/** + * A parser that extracts #include statements from a Reader. + * + * @author Adam Murdoch + * @author Curt Arnold + */ +public final class CParser extends AbstractParser implements Parser { + private final Vector includes = new Vector(); + private AbstractParserState newLineState; + /** + * + * + */ + public CParser() { + AbstractParserState quote = new FilenameState(this, new char[]{'"'}); + AbstractParserState bracket = new FilenameState(this, new char[]{'>'}); + AbstractParserState postE = new PostE(this, bracket, quote); + // + // nclude + // + AbstractParserState e = new LetterState(this, 'e', postE, null); + AbstractParserState d = new LetterState(this, 'd', e, null); + AbstractParserState u = new LetterState(this, 'u', d, null); + AbstractParserState l = new LetterState(this, 'l', u, null); + AbstractParserState c = new LetterState(this, 'c', l, null); + AbstractParserState n = new LetterState(this, 'n', c, null); + // + // mport is equivalent to nclude + // + AbstractParserState t = new LetterState(this, 't', postE, null); + AbstractParserState r = new LetterState(this, 'r', t, null); + AbstractParserState o = new LetterState(this, 'o', r, null); + AbstractParserState p = new LetterState(this, 'p', o, null); + AbstractParserState m = new LetterState(this, 'm', p, null); + // + // switch between + // + AbstractParserState n_m = new BranchState(this, new char[]{'n', 'm'}, + new AbstractParserState[]{n, m}, null); + AbstractParserState i = new WhitespaceOrLetterState(this, 'i', n_m); + newLineState = new LetterState(this, '#', i, null); + } + public void addFilename(String include) { + includes.addElement(include); + } + public String[] getIncludes() { + String[] retval = new String[includes.size()]; + includes.copyInto(retval); + return retval; + } + public AbstractParserState getNewLineState() { + return newLineState; + } + public void parse(Reader reader) throws IOException { + includes.setSize(0); + super.parse(reader); + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/CaseInsensitiveLetterState.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/CaseInsensitiveLetterState.java new file mode 100644 index 0000000000..a86700f166 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/CaseInsensitiveLetterState.java @@ -0,0 +1,87 @@ +/* + * + * Copyright 2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; + +/** + * This parser state checks consumed characters against a specific character + * (case insensitive). + * + * @author Curt Arnold + */ +public final class CaseInsensitiveLetterState + extends AbstractParserState { + /** + * Next state if a match is found. + */ + private final AbstractParserState nextState; + + /** + * Next state if not match is found. + */ + private final AbstractParserState noMatchState; + + /** + * Lower case version of character to match. + */ + private final char lowerLetter; + + /** + * Lower case version of character to match. + */ + private final char upperLetter; + + /** + * Constructor. + * + * @param parser + * parser + * @param matchLetter + * letter to match + * @param nextStateArg + * next state if a match on the letter + * @param noMatchStateArg + * state if no match on letter + */ + public CaseInsensitiveLetterState(final AbstractParser parser, + final char matchLetter, + final AbstractParserState nextStateArg, + final AbstractParserState noMatchStateArg) { + super(parser); + this.lowerLetter = Character.toLowerCase(matchLetter); + this.upperLetter = Character.toUpperCase(matchLetter); + this.nextState = nextStateArg; + this.noMatchState = noMatchStateArg; + } + + /** + * Consumes a character and returns the next state for the parser. + * + * @param ch + * next character + * @return the configured nextState if ch is the expected character or the + * configure noMatchState otherwise. + */ + public AbstractParserState consume(final char ch) { + if (ch == lowerLetter || ch == upperLetter) { + return nextState; + } + if (ch == '\n') { + getParser().getNewLineState(); + } + return noMatchState; + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/FilenameState.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/FilenameState.java new file mode 100644 index 0000000000..f33940b0a7 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/FilenameState.java @@ -0,0 +1,41 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; +public class FilenameState extends AbstractParserState { + private final StringBuffer buf = new StringBuffer(); + private final char[] terminators; + public FilenameState(AbstractParser parser, char[] terminators) { + super(parser); + this.terminators = (char[]) terminators.clone(); + } + public AbstractParserState consume(char ch) { + for (int i = 0; i < terminators.length; i++) { + if (ch == terminators[i]) { + getParser().addFilename(buf.toString()); + buf.setLength(0); + return null; + } + } + if (ch == '\n') { + buf.setLength(0); + return getParser().getNewLineState(); + } else { + buf.append(ch); + } + return this; + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/FortranParser.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/FortranParser.java new file mode 100644 index 0000000000..261e95ad81 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/FortranParser.java @@ -0,0 +1,106 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; + +import java.io.IOException; +import java.io.Reader; +import java.util.Vector; + +/** + * A parser that extracts INCLUDE statements from a Reader. + * + * @author Curt Arnold + */ +public final class FortranParser + extends AbstractParser + implements Parser { + /** + * List of included filenames. + */ + private final Vector includes = new Vector(); + + /** + * State that starts consuming content at the beginning of a line. + */ + private final AbstractParserState newLineState; + + /** + * Default constructor. + * + */ + public FortranParser() { + AbstractParserState filename = new FilenameState(this, new char[] {'\'', + '/'}); + AbstractParserState apos = new WhitespaceOrLetterState(this, '\'', + filename); + AbstractParserState blank = new LetterState(this, ' ', apos, null); + AbstractParserState e = new CaseInsensitiveLetterState(this, 'E', + blank, null); + AbstractParserState d = new CaseInsensitiveLetterState(this, 'D', e, + null); + AbstractParserState u = new CaseInsensitiveLetterState(this, 'U', d, + null); + AbstractParserState l = new CaseInsensitiveLetterState(this, 'L', u, + null); + AbstractParserState c = new CaseInsensitiveLetterState(this, 'C', l, + null); + AbstractParserState n = new CaseInsensitiveLetterState(this, 'N', c, + null); + newLineState = new WhitespaceOrCaseInsensitiveLetterState(this, 'I', n); + } + + /** + * Called by FilenameState at completion of file name production. + * + * @param include + * include file name + */ + public void addFilename(final String include) { + includes.addElement(include); + } + + /** + * Gets collection of include file names encountered in parse. + * @return include file names + */ + public String[] getIncludes() { + String[] retval = new String[includes.size()]; + includes.copyInto(retval); + return retval; + } + + /** + * Get the state for the beginning of a new line. + * @return start of line state + */ + public AbstractParserState getNewLineState() { + return newLineState; + } + + /** + * Collects all included files from the content of the reader. + * + * @param reader + * character reader containing a FORTRAN source module + * @throws IOException + * throw if I/O error during parse + */ + public void parse(final Reader reader) throws IOException { + includes.setSize(0); + super.parse(reader); + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/LetterState.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/LetterState.java new file mode 100644 index 0000000000..945ae91233 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/LetterState.java @@ -0,0 +1,80 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; + +/** + * This parser state checks consumed characters against a specific character. + * + * @author Curt Arnold + */ +public final class LetterState + extends AbstractParserState { + /** + * Next state if a match is found. + */ + private final AbstractParserState nextState; + + /** + * Next state if not match is found. + */ + private final AbstractParserState noMatchState; + + /** + * Character to match. + */ + private final char thisLetter; + + /** + * Constructor. + * + * @param parser + * parser + * @param matchLetter + * letter to match + * @param nextStateArg + * next state if a match on the letter + * @param noMatchStateArg + * state if no match on letter + */ + public LetterState(final AbstractParser parser, + final char matchLetter, + final AbstractParserState nextStateArg, + final AbstractParserState noMatchStateArg) { + super(parser); + this.thisLetter = matchLetter; + this.nextState = nextStateArg; + this.noMatchState = noMatchStateArg; + } + + /** + * Consumes a character and returns the next state for the parser. + * + * @param ch + * next character + * @return the configured nextState if ch is the expected character or the + * configure noMatchState otherwise. + */ + public AbstractParserState consume(final char ch) { + if (ch == thisLetter) { + return nextState; + } + if (ch == '\n') { + getParser().getNewLineState(); + } + return noMatchState; + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/Parser.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/Parser.java new file mode 100644 index 0000000000..cf759fc7d5 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/Parser.java @@ -0,0 +1,28 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; +import java.io.IOException; +import java.io.Reader; +/** + * A parser that extracts #include statements from a Reader. + * + * @author Curt Arnold + */ +public interface Parser { + String[] getIncludes(); + void parse(Reader reader) throws IOException; +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/PostE.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/PostE.java new file mode 100644 index 0000000000..fe225fb35a --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/PostE.java @@ -0,0 +1,41 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; +public class PostE extends AbstractParserState { + private AbstractParserState bracket; + private AbstractParserState quote; + public PostE(CParser parser, AbstractParserState bracket, + AbstractParserState quote) { + super(parser); + this.bracket = bracket; + this.quote = quote; + } + public AbstractParserState consume(char ch) { + switch (ch) { + case ' ' : + case '\t' : + return this; + case '<' : + return bracket; + case '"' : + return quote; + case '\n' : + return getParser().getNewLineState(); + } + return null; + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/WhitespaceOrCaseInsensitiveLetterState.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/WhitespaceOrCaseInsensitiveLetterState.java new file mode 100644 index 0000000000..9d42feeaad --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/WhitespaceOrCaseInsensitiveLetterState.java @@ -0,0 +1,83 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; + +/** + * This parser state checks consumed characters against a specific character + * (case insensitive) or whitespace. + * + * @author Curt Arnold + */ +public final class WhitespaceOrCaseInsensitiveLetterState + extends + AbstractParserState { + /** + * Next state if the character is found. + */ + private final AbstractParserState nextState; + + /** + * Character to match (lower case). + */ + private final char lowerLetter; + + /** + * Character to match (upper case). + */ + private final char upperLetter; + + /** + * Constructor. + * + * @param parser + * parser + * @param matchLetter + * letter to match + * @param nextStateArg + * next state if a match on the letter + */ + public WhitespaceOrCaseInsensitiveLetterState(final AbstractParser parser, + final char matchLetter, + final AbstractParserState + nextStateArg) { + super(parser); + this.lowerLetter = Character.toLowerCase(matchLetter); + this.upperLetter = Character.toUpperCase(matchLetter); + this.nextState = nextStateArg; + } + + /** + * Consumes a character and returns the next state for the parser. + * + * @param ch + * next character + * @return the configured nextState if ch is the expected character or the + * configure noMatchState otherwise. + */ + public AbstractParserState consume(final char ch) { + if (ch == lowerLetter || ch == upperLetter) { + return nextState; + } + if (ch == ' ' || ch == '\t') { + return this; + } + if (ch == '\n') { + getParser().getNewLineState(); + } + return null; + } +} diff --git a/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/WhitespaceOrLetterState.java b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/WhitespaceOrLetterState.java new file mode 100644 index 0000000000..824abe6b68 --- /dev/null +++ b/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/parser/WhitespaceOrLetterState.java @@ -0,0 +1,75 @@ +/* + * + * Copyright 2002-2004 The Ant-Contrib project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.antcontrib.cpptasks.parser; + +/** + * This parser state checks consumed characters against a specific character or + * whitespace. + * + * @author Curt Arnold + */ +public final class WhitespaceOrLetterState + extends AbstractParserState { + /** + * Next state if the character is found. + */ + private final AbstractParserState nextState; + + /** + * Character to match. + */ + private final char thisLetter; + + /** + * Constructor. + * + * @param parser + * parser + * @param matchLetter + * letter to match + * @param nextStateArg + * next state if a match on the letter + */ + public WhitespaceOrLetterState(final AbstractParser parser, + final char matchLetter, + final AbstractParserState nextStateArg) { + super(parser); + this.thisLetter = matchLetter; + this.nextState = nextStateArg; + } + + /** + * Consumes a character and returns the next state for the parser. + * + * @param ch + * next character @returns the configured nextState if ch is the + * expected character or the configure noMatchState otherwise. + * @return next state + */ + public AbstractParserState consume(final char ch) { + if (ch == thisLetter) { + return nextState; + } + if (ch == ' ' || ch == '\t') { + return this; + } + if (ch == '\n') { + getParser().getNewLineState(); + } + return null; + } +} -- cgit v1.2.3