Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/rxm.git
source/grammar/Query.g
/* Map lines affect the tree depth differently.
*/
-map : tableMap | columnMap | innerMap | outerMap;
-tableMap : table T_MAP node ;
-columnMap: column T_MAP node ;
-innerMap : tableColumn T_INNER tableColumn ;
-outerMap : tableColumn T_OUTER tableColumn ;
+map : tableMap | columnMap | attributeMap | innerMap | outerMap;
+tableMap : table T_MAP path ;
+columnMap : column T_MAP path ;
+attributeMap: column T_MAP attribute ;
+innerMap : tableColumn T_INNER tableColumn ;
+outerMap : tableColumn T_OUTER tableColumn ;
-include : T_IMPORT T_ID ;
+include : T_IMPORT T_ID ;
table : T_ID ;
column : T_PERIOD T_ID ;
tableColumn: table column ;
attribute : T_AT T_ID ;
element : T_ID ;
-path : element T_SLASH element ;
-
-node : element | attribute | path ;
+elementPath: element T_SLASH element ;
+path : element | elementPath;
/* Define the WHERE clause.
source/java/com/whitemagicsoftware/rxm/Parser.java
}
+ @Override
+ public synchronized void enterAttributeMap(
+ QueryParser.AttributeMapContext ctx ) {
+ addLeaf( ctx );
+ }
+
/**
* Invoked when parsing <code>^</code>. Changes context tree to its parent.
source/java/com/whitemagicsoftware/rxm/tree/TransformationTree.java
/**
- * Depth-first traversal.
+ * Depth-first traversal. This converts all payload instances in the tree
+ * to their transformed equivalent.
+ *
+ * @param tree The tree to transform.
+ * @param depth Indicates number of spaces for indentation.
+ *
+ * @return The fully transformed text.
*/
protected String toString( Tree<T> tree, int depth ) {
*
* @param payload The data to transform into its opening text.
+ * @return The opening for the transformed payload text.
*/
protected abstract String start( T payload );
/**
* Called when exiting a new hierarchy in the transformation tree.
*
* @param payload The data to transform into its closing text.
+ * @return The closing for the transformed payload text.
*/
protected abstract String stop( T payload );
source/java/com/whitemagicsoftware/rxm/tree/xml/ASTParserRuleContext.java
/**
+ * Formats the start of the XMLELEMENT call.
*
* @param name The name assigned to the element.
+ *
+ * @return The text used for SQL/XML XMLELEMENT calls.
*/
protected String startElement( String name ) {
return String.format( "XMLELEMENT( NAME \"%s\"", name );
}
/**
+ * Formats the start of the XMLATTRIBUTES call.
+ *
* @param name The name assigned to the attribute.
+ *
+ * @return The text used for SQL/XML XMLATTRIBUTES calls.
*/
protected String startAttributes( String name ) {
return String.format( "XMLATTRIBUTES( %s AS ", name );
- }
-
- /**
- * Casts the payload into the proper context, then extracts the element.
- * This should not be called if the wrapped payload does not have an
- * element.
- *
- * @return The element associated with the root parser rule.
- */
- protected QueryParser.ElementContext getElementContext() {
- return ((QueryParser.RootContext)getParserRuleContext()).element();
}
/**
* Subclass override this to change the content returned by the parser
* rule. This is called during transformation.
+ *
+ * @return The token containing a transformed string for the wrapped opening.
*/
@Override
public abstract Token getStart();
/**
* Subclass override this to change the content returned by the parser
* rule. This is called during transformation.
+ *
+ * @return The token containing a transformed string for the wrapped closing.
*/
@Override
source/java/com/whitemagicsoftware/rxm/tree/xml/AttributeMapContext.java
+package com.whitemagicsoftware.rxm.tree.xml;
+
+import org.antlr.v4.runtime.ParserRuleContext;
+
+import com.whitemagicsoftware.rxm.grammar.QueryParser;
+
+public class AttributeMapContext extends ASTParserRuleContext {
+ public AttributeMapContext( ParserRuleContext ctx ) {
+ super( ctx );
+ }
+
+ /**
+ * Opens the XMLATTRIBUTES.
+ */
+ @Override
+ public Token getStart() {
+ String e = getParserRuleContext().getText();
+
+ String s = String.format( ",%s ", startAttributes( e ) );
+ return new Token( s );
+ }
+
+ /**
+ * Closes the ATTRIBUTES.
+ */
+ @Override
+ public Token getStop() {
+ return new Token( ")" );
+ }
+}
+
source/java/com/whitemagicsoftware/rxm/tree/xml/ColumnMapContext.java
import org.antlr.v4.runtime.ParserRuleContext;
+import com.whitemagicsoftware.rxm.grammar.QueryParser;
+
public class ColumnMapContext extends ASTParserRuleContext {
public ColumnMapContext( ParserRuleContext ctx ) {
super( ctx );
}
+ /**
+ * Opens the XMLELEMENT.
+ */
@Override
public Token getStart() {
- return new Token( "COLUMN MAP:" + toString() );
+ QueryParser.PathContext path = getPathContext();
+ String e = path.getText();
+
+ String s = String.format( ",%s, %s", startElement( e ), getColumnName() );
+ return new Token( s );
+ }
+
+ /**
+ * This will return column name without the leading period.
+ *
+ * @return The column name, without a leading period.
+ */
+ protected String getColumnName() {
+ // The first child (index 0) is the leading period.
+ // The second child (index 1) is the column name.
+ return getColumnContext().getChild(1).getText();
}
+ /**
+ * Closes the XMLELEMENT.
+ */
@Override
public Token getStop() {
return new Token( ")" );
+ }
+
+ /**
+ * Casts the payload into the proper context, then extracts the path.
+ *
+ * @return The path associated with the column map parser rule.
+ */
+ protected QueryParser.PathContext getPathContext() {
+ return ((QueryParser.ColumnMapContext)getParserRuleContext()).path();
+ }
+
+ /**
+ * Casts the payload into the proper context, then extracts the column.
+ *
+ * @return The column associated with the column map parser rule.
+ */
+ protected QueryParser.ColumnContext getColumnContext() {
+ return ((QueryParser.ColumnMapContext)getParserRuleContext()).column();
}
}
source/java/com/whitemagicsoftware/rxm/tree/xml/RootContext.java
return true;
}
+
+ /**
+ * Casts the payload into the proper context, then extracts the element.
+ *
+ * @return The element associated with the root parser rule.
+ */
+ protected QueryParser.ElementContext getElementContext() {
+ return ((QueryParser.RootContext)getParserRuleContext()).element();
+ }
}
source/java/com/whitemagicsoftware/rxm/tree/xml/TableMapContext.java
import org.antlr.v4.runtime.ParserRuleContext;
+import com.whitemagicsoftware.rxm.grammar.QueryParser;
+
/**
* Transforms <code>element &gt; element</code>.
@Override
public Token getStart() {
- QueryParser.ElementContext element = getElementContext();
- String e = element.getText();
+ QueryParser.PathContext path = getPathContext();
+ String e = path.getText();
String s = String.format( ",%s", startElement( e ) );
public Token getStop() {
return new Token( ")" );
+ }
+
+ /**
+ * Casts the payload into the proper context, then extracts the path.
+ *
+ * @return The path associated with the table map parser rule.
+ */
+ protected QueryParser.PathContext getPathContext() {
+ return ((QueryParser.TableMapContext)getParserRuleContext()).path();
}
}
source/java/com/whitemagicsoftware/rxm/tree/xml/Token.java
+package com.whitemagicsoftware.rxm.tree.xml;
+
+import org.antlr.v4.runtime.CommonToken;
+import static org.antlr.v4.runtime.Token.MIN_USER_TOKEN_TYPE;
+
+/**
+ * Avoids having to dupliate common token functionality across the different
+ * parser rule contexts.
+ */
+public class Token extends CommonToken {
+ public Token( String text ) {
+ super( MIN_USER_TOKEN_TYPE, text );
+ }
+
+ public String toString() {
+ return getText();
+ }
+}
+
test/people.rxm
.*,
^,
-company > company,
company.person_id -> person.person_id,
+company > company,
.id > @id,
.legal_name > name,

Abstracted tokens. Revised table, column, and attribute maps to generate code closer to the requirements.

Author Dave Jarvis <email>
Date 2015-03-07 19:59:09 GMT-0800
Commit 381282746452f05c249caca7a8c8bd32433938e7
Parent d253c1b
Delta 150 lines added, 25 lines removed, 125-line increase