| /*=============================================================================# |
| # Copyright (c) 2012, 2018 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.docmlet.tex.core.ast; |
| |
| import static org.eclipse.statet.docmlet.tex.core.ast.ITexAstStatusConstants.STATUS2_GROUP_NOT_CLOSED; |
| |
| import java.util.List; |
| |
| import org.eclipse.statet.jcommons.text.core.BasicTextRegion; |
| import org.eclipse.statet.jcommons.text.core.TextRegion; |
| |
| import org.eclipse.statet.docmlet.tex.core.ITexProblemConstants; |
| import org.eclipse.statet.docmlet.tex.core.commands.Argument; |
| import org.eclipse.statet.docmlet.tex.core.commands.IEnvDefinitions; |
| import org.eclipse.statet.ltk.ast.core.Ast; |
| |
| |
| public class TexAst extends Ast { |
| |
| |
| /** |
| * Definitions of LaTeX AST node types |
| */ |
| public enum NodeType { |
| |
| |
| SOURCELINES, |
| COMMENT, |
| ERROR, |
| EMBEDDED, |
| |
| GROUP, |
| ENVIRONMENT, |
| VERBATIM, |
| MATH, |
| |
| CONTROL, |
| LABEL, |
| TEXT, |
| |
| } |
| |
| |
| /** |
| * Resolves the argument values (AST nodes) to the arguments defined by the command. |
| * The command have to be provided by the control node ({@link ControlNode#getCommand()}). |
| * |
| * @param node the control node |
| * @return array with the resolved arguments (items can be <code>null</code>) |
| */ |
| public static TexAstNode[] resolveArguments(final ControlNode node) { |
| final List<Argument> arguments= node.getCommand().getArguments(); |
| final TexAstNode[] resolved= new TexAstNode[arguments.size()]; |
| int idxArgs= 0, idxValues= 0; |
| while (idxArgs < resolved.length && idxValues < node.getChildCount()) { |
| final TexAstNode child= node.getChild(idxValues); |
| if ((arguments.get(idxArgs).getType() & Argument.OPTIONAL) != 0) { |
| if (child.getText() == "[") { //$NON-NLS-1$ |
| idxValues++; |
| resolved[idxArgs++]= child; |
| continue; |
| } |
| else { |
| idxArgs++; |
| continue; |
| } |
| } |
| else { |
| if (child.getText() == "[") { //$NON-NLS-1$ |
| idxValues++; |
| continue; |
| } |
| else { |
| idxValues++; |
| resolved[idxArgs++]= child; |
| } |
| } |
| } |
| return resolved; |
| } |
| |
| /** |
| * Returns the index in the array for the node at the specified offset |
| * |
| * @param nodes array with nodes (items can be <code>null</code>) |
| * @param offset the offset of the searched node |
| * @return the index in the array if found, otherwise -1 |
| */ |
| public static int getIndexAt(final TexAstNode[] nodes, final int offset) { |
| for (int i= 0; i < nodes.length; i++) { |
| if (nodes[i] != null) { |
| if (offset < nodes[i].getStartOffset()) { |
| return -1; |
| } |
| if (offset <= nodes[i].getEndOffset()) { |
| return i; |
| } |
| } |
| } |
| return -1; |
| } |
| |
| /** |
| * At moment only for groups |
| * |
| * @param node |
| * @return |
| */ |
| public static TextRegion getInnerRegion(final TexAstNode node) { |
| if ((node.getStatusCode() & ITexProblemConstants.MASK_12) == STATUS2_GROUP_NOT_CLOSED) { |
| return new BasicTextRegion(node.getStartOffset() + 1, node.getEndOffset()); |
| } |
| else { |
| return new BasicTextRegion(node.getStartOffset() + 1, node.getEndOffset() - 1); |
| } |
| } |
| |
| public static Environment getDocumentNode(final TexAstNode node) { |
| final int count= node.getChildCount(); |
| for (int i= 0; i < count; i++) { |
| final TexAstNode child= node.getChild(i); |
| if (child.getNodeType() == NodeType.ENVIRONMENT |
| && ((Environment) node).getBeginNode().getCommand() == IEnvDefinitions.ENV_document_BEGIN) { |
| return (Environment) child; |
| } |
| } |
| return null; |
| } |
| |
| |
| } |