Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/rxm.git

Removed unused variable. Added equivalent functionality to tree -- seeking the root node.

Author Dave Jarvis <email>
Date 2015-03-05 20:52:40 GMT-0800
Commit 8b6d0a07092c4f16c741e19ff5a88c597d15f705
Parent df0820f
source/java/com/whitemagicsoftware/rxm/Parser.java
/**
- * Parses the expression into an abstract tree.
+ * Parses the Relational eXpression Map (<b>rxm</b>) into a tree.
*/
public class Parser extends QueryBaseListener {
private static int INDENT = 2;
+ /** Map for containing modules. */
private RXM rxm;
-
- /** Used to convert linear map to hierarchical form. */
- private List<String> statements = new ArrayList<String>();
-
- /** The tree constructed while walking the <b>rxm</b>. */
- private Tree<ParserRuleContext> queryTree;
- /** Current context within queryTree. */
+ /** Current context. */
private Tree<ParserRuleContext> contextTree;
}
+ /**
+ * Parses the given <b>rxm</b> text. This creates a tree that is used
+ * for context when building the query.
+ *
+ * @param rxm The <b>rxm</b> string to parse.
+ */
public void parse( String rxm ) {
QueryLexer lexer = new QueryLexer( new ANTLRInputStream( rxm ) );
CommonTokenStream tokens = new CommonTokenStream( lexer );
QueryParser parser = new QueryParser( tokens );
QueryParser.QueryContext ctx = parser.query();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk( this, ctx );
- }
-
- @Override
- public synchronized void enterQuery( QueryParser.QueryContext ctx ) {
- setQueryTree( createTree( ctx ) );
}
+ /**
+ * Adds the root context node to the top-most part of the tree. This
+ * sets the context tree to the root context node.
+ *
+ * @param ctx The root context node.
+ */
@Override
public synchronized void enterRoot( QueryParser.RootContext ctx ) {
- setContextTree( getQueryTree().addLeaf( ctx ) );
+ setContextTree( createTree( ctx ) );
}
+ /**
+ * Invoked when parsing <code>table &gt; node</code>. Adds a new leaf to
+ * the context tree, then changes the context tree to the newly added leaf.
+ */
@Override
public synchronized void enterTableMap( QueryParser.TableMapContext ctx ) {
setContextTree( getContextTree().addLeaf( ctx ) );
}
+ /**
+ * Invoked when parsing <code>column &gt; node</code>. Adds a new leaf to
+ * the context tree.
+ */
@Override
public synchronized void enterColumnMap( QueryParser.ColumnMapContext ctx ) {
getContextTree().addLeaf( ctx );
}
+ /**
+ * Invoked when parsing <code>^</code>. Changes context tree to its parent.
+ * If the parent doesn't exist, this will fail silently.
+ */
@Override
public synchronized void enterPop( QueryParser.PopContext ctx ) {
- setContextTree( getContextTree().getParent() );
+ Tree<ParserRuleContext> parent = getContextTree().getParent();
+
+ if( parent != null ) {
+ setContextTree( getContextTree().getParent() );
+ }
}
+ /**
+ * Invoked when there are no more <b>rxm</b> tokens to parse.
+ */
@Override
public synchronized void exitQuery( QueryParser.QueryContext ctx ) {
@Override
public String toString() {
- return toString( getQueryTree().getLeaves().get(0), INDENT );
+ return toString( getContextTree().getRoot(), INDENT );
}
}
+ /**
+ * Creates a string of spaces that is spaces spaces long.
+ *
+ * @param spaces The number of spaces to add to the string.
+ */
private String indent( int spaces ) {
String empty = CharBuffer.allocate( spaces ).toString();
private RXM getRXM() {
return this.rxm;
- }
-
- private List<String> getStatements() {
- return this.statements;
}
+ /**
+ * Changes where the next mapped items will be attached.
+ */
private void setContextTree( Tree<ParserRuleContext> contextTree ) {
this.contextTree = contextTree;
}
+ /**
+ * Returns where to attach upcoming map items.
+ */
private Tree<ParserRuleContext> getContextTree() {
return this.contextTree;
- }
-
- private void setQueryTree( Tree<ParserRuleContext> queryTree ) {
- this.queryTree = queryTree;
- }
-
- private Tree<ParserRuleContext> getQueryTree() {
- return this.queryTree;
}
source/java/com/whitemagicsoftware/rxm/Tree.java
/**
+ * Finds the root to this tree by iterating over this tree's parent nodes.
+ *
+ * @return The root of this tree.
+ */
+ public Tree<T> getRoot() {
+ Tree<T> root = this;
+
+ while( root.getParent() != null ) {
+ root = getParent();
+ }
+
+ return root;
+ }
+
+ /**
* Adds a new leaf to this tree.
*
Delta 63 lines added, 28 lines removed, 35-line increase