📄 toplevelparser.java
字号:
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Jamon code, released February, 2003. * * The Initial Developer of the Original Code is Ian Robertson. Portions * created by Ian Robertson are Copyright (C) 2005 Ian Robertson. All Rights * Reserved. * * Contributor(s): */package org.jamon.parser;import java.io.IOException;import java.io.Reader;import org.jamon.ParserError;import org.jamon.ParserErrors;import org.jamon.TemplateLocation;import org.jamon.codegen.AnnotationType;import org.jamon.node.AbsMethodNode;import org.jamon.node.AbstractPathNode;import org.jamon.node.AliasDefNode;import org.jamon.node.AliasesNode;import org.jamon.node.AnnotationNode;import org.jamon.node.ClassNode;import org.jamon.node.EscapeDirectiveNode;import org.jamon.node.ExtendsNode;import org.jamon.node.ImplementNode;import org.jamon.node.ImplementsNode;import org.jamon.node.ImportsNode;import org.jamon.node.Location;import org.jamon.node.ParentMarkerNode;import org.jamon.node.TopNode;public class TopLevelParser extends AbstractBodyParser<TopNode>{ public static final String BAD_ABSMETH_CONTENT = "<%absmeth> sections can only contain <%args> and <%frag> blocks"; public static final String EXPECTING_SEMI = "Expecting ';'"; public static final String EXPECTING_ARROW = "Expecting '=' or '=>'"; public static final String MALFORMED_EXTENDS_TAG_ERROR = "Malformed <%extends ...> tag"; public static final String MALFORMED_ANNOTATE_TAG_ERROR = "Malformed <%annotate...> tag"; public static final String UNRECOGNIZED_ANNOTATION_TYPE_ERROR = "Unrecognized annotation type"; private static final String BAD_ALIASES_CLOSE_TAG = "Malformed </%alias> tag"; private static final String BAD_ABS_METHOD_CLOSE_TAG = "Malformed </%absmeth> tag"; public static final String EXPECTING_IMPLEMENTS_CLOSE = "Expecting class name or </%implements>"; public static final String EXPECTING_IMPORTS_CLOSE = "Expecting import or </%import>"; public TopLevelParser(TemplateLocation p_location, Reader p_reader) { super(new TopNode(new Location(p_location, 1, 1)), new PositionalPushbackReader(p_location, p_reader, 2), new ParserErrors()); } @Override public AbstractBodyParser<TopNode> parse() throws IOException { super.parse(); if (m_errors.hasErrors()) { throw m_errors; } return this; } @Override protected void handleMethodTag(Location p_tagLocation) throws IOException { if (soakWhitespace()) { String name = readIdentifier(true); if (checkForTagClosure(p_tagLocation)) { m_root.addSubNode( new MethodParser(name, p_tagLocation, m_reader, m_errors) .parse() .getRootNode()); } } else { addError(p_tagLocation, "malformed <%method methodName> tag"); } } @Override protected void handleOverrideTag(Location p_tagLocation) throws IOException { if (soakWhitespace()) { String name = readIdentifier(true); if (checkForTagClosure(p_tagLocation)) { m_root.addSubNode( new OverrideParser(name, p_tagLocation, m_reader, m_errors) .parse() .getRootNode()); } } else { addError(p_tagLocation, "malformed <%override methodName> tag"); } } @Override protected void handleDefTag(Location p_tagLocation) throws IOException { if (soakWhitespace()) { String name = readIdentifier(true); if (checkForTagClosure(p_tagLocation)) { m_root.addSubNode( new DefParser(name, p_tagLocation, m_reader, m_errors) .parse() .getRootNode()); } } else { addError(p_tagLocation, "malformed <%def defName> tag"); } } @Override protected void handleClassTag(Location p_tagLocation) throws IOException { if (checkForTagClosure(p_tagLocation)) { m_root.addSubNode( new ClassNode( p_tagLocation, readUntil("</%class>", p_tagLocation))); soakWhitespace(); } } @Override protected void handleExtendsTag(Location p_tagLocation) throws IOException { if(soakWhitespace()) { m_root.addSubNode( new ExtendsNode(p_tagLocation, parsePath())); soakWhitespace(); checkForTagClosure(m_reader.getLocation()); soakWhitespace(); } else { addError(p_tagLocation, MALFORMED_EXTENDS_TAG_ERROR); } } @Override protected void handleImplementsTag(Location p_tagLocation) throws IOException { if (checkForTagClosure(p_tagLocation)) { ImplementsNode implementsNode = new ImplementsNode(p_tagLocation); m_root.addSubNode(implementsNode); while(true) { soakWhitespace(); Location location = m_reader.getNextLocation(); if (readChar('<')) { if (!checkToken("/%implements>")) { addError(location, EXPECTING_IMPLEMENTS_CLOSE); } soakWhitespace(); return; } String className = readClassName(m_reader.getCurrentNodeLocation()); if (className.length() == 0) { addError(location, EXPECTING_IMPLEMENTS_CLOSE); return; } if (!readChar(';')) { addError(m_reader.getNextLocation(), EXPECTING_SEMI); } implementsNode.addImplement( new ImplementNode(location, className)); } } } @Override protected void handleImportTag(Location p_tagLocation) throws IOException { if (checkForTagClosure(p_tagLocation)) { ImportsNode importsNode = new ImportsNode(p_tagLocation); m_root.addSubNode(importsNode); while(true) { soakWhitespace(); Location location = m_reader.getNextLocation(); if (readChar('<')) { if (!checkToken("/%import>")) { addError(location, EXPECTING_IMPORTS_CLOSE); } soakWhitespace(); return; } try { importsNode.addImport( new ImportParser(m_reader, m_errors).parse().getNode()); } catch (ParserError e) { addError(e); addError(m_reader.getLocation(), EXPECTING_IMPORTS_CLOSE); return; } soakWhitespace(); if (!readChar(';')) { addError(m_reader.getNextLocation(), EXPECTING_SEMI); } } } } @Override protected void handleAliasesTag(Location p_tagLocation) throws IOException { checkForTagClosure(p_tagLocation); AliasesNode aliases = new AliasesNode(p_tagLocation);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -