001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package eu.cqse.check.framework.shallowparser.languages.python;
018
019import static eu.cqse.check.framework.scanner.ETokenType.CLASS;
020import static eu.cqse.check.framework.scanner.ETokenType.DEDENT;
021import static eu.cqse.check.framework.scanner.ETokenType.DEF;
022import static eu.cqse.check.framework.scanner.ETokenType.EOL;
023import static eu.cqse.check.framework.scanner.ETokenType.LAMBDA;
024import static eu.cqse.check.framework.scanner.ETokenType.RBRACE;
025import static eu.cqse.check.framework.scanner.ETokenType.RPAREN;
026import static eu.cqse.check.framework.shallowparser.languages.python.PythonShallowParser.EPythonParserStates.ANY;
027
028import java.util.EnumSet;
029import java.util.List;
030import java.util.Stack;
031
032import eu.cqse.check.framework.scanner.ETokenType;
033import eu.cqse.check.framework.scanner.IToken;
034import eu.cqse.check.framework.shallowparser.languages.base.LineBasedStatementRecognizerBase;
035import eu.cqse.check.framework.shallowparser.languages.python.PythonShallowParser.EPythonParserStates;
036
037/**
038 * Recognizer for simple statements in Python.
039 */
040public class PythonSimpleStatementRecognizer extends LineBasedStatementRecognizerBase<EPythonParserStates> {
041
042        /**
043         * Tokens that indicate that subparsing at the token's location is necessary.
044         */
045        private static final EnumSet<ETokenType> SUB_PARSE_TOKENS = EnumSet.of(DEF, CLASS, LAMBDA);
046
047        /** Tokens that start a new statement. */
048        private static final EnumSet<ETokenType> NEW_STATEMENT_TOKENS = EnumSet.of(EOL, DEDENT, RPAREN, RBRACE);
049
050        /** {@inheritDoc} */
051        @Override
052        protected EPythonParserStates getSubParseState() {
053                return ANY;
054        }
055
056        /** {@inheritDoc} */
057        @Override
058        protected boolean tokenStartsSubParse(ETokenType tokenType, List<IToken> tokens, int offset,
059                        Stack<ETokenType> expectedClosing) {
060                return SUB_PARSE_TOKENS.contains(tokenType);
061        }
062
063        /** {@inheritDoc} */
064        @Override
065        protected boolean startsNewStatement(IToken token, IToken lastToken) {
066                return NEW_STATEMENT_TOKENS.contains(token.getType());
067        }
068}