Skip to content

Commit

Permalink
Added AbstractExpression and TreeRenderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Jul 1, 2014
1 parent 2c8d00d commit 9d0908b
Show file tree
Hide file tree
Showing 16 changed files with 500 additions and 10 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>co.icecave</groupId>
<artifactId>dialekt</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
<packaging>jar</packaging>

<name>Dialekt</name>
Expand Down Expand Up @@ -34,6 +34,12 @@
</properties>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/co/icecave/dialekt/ast/AbstractExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package co.icecave.dialekt.ast;

import co.icecave.dialekt.parser.Token;

/**
* A base class providing common functionality for expressions.
*/
public abstract class AbstractExpression implements ExpressionInterface
{
/**
* Fetch the first token from the source that is part of this expression.
*
* @return The first token from this expression.
*/
public Token firstToken()
{
return this.firstToken;
}

/**
* Fetch the last token from the source that is part of this expression.
*
* @return The last token from this expression.
*/
public Token lastToken()
{
return this.lastToken;
}

/**
* Set the delimiting tokens for this expression.
*
* @param firstToken The first token from this expression.
* @param lastToken The last token from this expression.
*/
public void setTokens(Token firstToken, Token lastToken)
{
this.firstToken = firstToken;
this.lastToken = lastToken;
}

private Token firstToken;
private Token lastToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
/**
* A base class providing common functionality for polyadic operators.
*/
public abstract class AbstractPolyadicOperator implements ExpressionInterface
public abstract class AbstractPolyadicExpression extends AbstractExpression
{
/**
* @param child,... One or more children to add to this operator.
*/
public AbstractPolyadicOperator(ExpressionInterface... children)
public AbstractPolyadicExpression(ExpressionInterface... children)
{
this.children = new ArrayList<ExpressionInterface>(
Arrays.asList(children)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/icecave/dialekt/ast/EmptyExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* An AST node that represents the logical OR operator.
*/
public class EmptyExpression implements ExpressionInterface
public class EmptyExpression extends AbstractExpression
{
/**
* Pass this node to the appropriate method on the given visitor.
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/co/icecave/dialekt/ast/ExpressionInterface.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package co.icecave.dialekt.ast;

import co.icecave.dialekt.parser.Token;

/**
* An AST node that is an expression.
*
* Not all nodes in the tree represent an entire (sub-)expression.
*/
public interface ExpressionInterface extends NodeInterface
{
/**
* Fetch the first token from the source that is part of this expression.
*
* @return The first token from this expression.
*/
public Token firstToken();

/**
* Fetch the last token from the source that is part of this expression.
*
* @return The last token from this expression.
*/
public Token lastToken();
}
2 changes: 1 addition & 1 deletion src/main/java/co/icecave/dialekt/ast/LogicalAnd.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* An AST node that represents the logical OR operator.
*/
public class LogicalAnd extends AbstractPolyadicOperator
public class LogicalAnd extends AbstractPolyadicExpression
{
/**
* @param child,... One or more children to add to this operator.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/icecave/dialekt/ast/LogicalNot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* An AST node that represents the logical NOT operator.
*/
public class LogicalNot implements ExpressionInterface
public class LogicalNot extends AbstractExpression
{
/**
* @param child The expression being inverted by the NOT operator.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/icecave/dialekt/ast/LogicalOr.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* An AST node that represents the logical OR operator.
*/
public class LogicalOr extends AbstractPolyadicOperator
public class LogicalOr extends AbstractPolyadicExpression
{
/**
* @param child,... One or more children to add to this operator.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/icecave/dialekt/ast/Pattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* An AST node that represents a pattern-match expression.
*/
public class Pattern implements ExpressionInterface
public class Pattern extends AbstractExpression
{
/**
* @param child,... One or more children to add to this operator.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/icecave/dialekt/ast/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* An AST node that represents a literal tag expression.
*/
public class Tag implements ExpressionInterface
public class Tag extends AbstractExpression
{
/**
* @param name The tag name.
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/co/icecave/dialekt/parser/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package co.icecave.dialekt.parser;

public class Token
{
// const WILDCARD_CHARACTER = '*';

// const LOGICAL_AND = 1;
// const LOGICAL_OR = 2;
// const LOGICAL_NOT = 3;
// const STRING = 4;
// const OPEN_BRACKET = 6;
// const CLOSE_BRACKET = 7;

// public function __construct(
// $type,
// $value,
// $startOffset,
// $endOffset,
// $lineNumber,
// $columnNumber
// ) {
// $this->type = $type;
// $this->value = $value;
// $this->startOffset = $startOffset;
// $this->endOffset = $endOffset;
// $this->lineNumber = $lineNumber;
// $this->columnNumber = $columnNumber;
// }

// public static function typeDescription($type)
// {
// switch ($type) {
// case self::LOGICAL_AND:
// return 'AND operator';
// case self::LOGICAL_OR:
// return 'OR operator';
// case self::LOGICAL_NOT:
// return 'NOT operator';
// case self::STRING:
// return 'tag';
// case self::OPEN_BRACKET:
// return 'open bracket';
// case self::CLOSE_BRACKET:
// return 'close bracket';
// };

// throw new LogicException('Unknown type.');
// }

// public $type;
// public $value;
// public $startOffset;
// public $endOffset;
// public $lineNumber;
// public $columnNumber;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Interface for node visitors.
*/
public class ExpressionRenderer implements VisitorInterface<String>
public class ExpressionRenderer implements RendererInterface, VisitorInterface<String>
{
public ExpressionRenderer()
{
Expand Down Expand Up @@ -47,6 +47,8 @@ public String render(ExpressionInterface expression)
/**
* Visit a LogicalAnd node.
*
* @internal
*
* @param node The node to visit.
*/
public String visit(LogicalAnd node)
Expand All @@ -57,6 +59,8 @@ public String visit(LogicalAnd node)
/**
* Visit a LogicalOr node.
*
* @internal
*
* @param node The node to visit.
*/
public String visit(LogicalOr node)
Expand All @@ -67,6 +71,8 @@ public String visit(LogicalOr node)
/**
* Visit a LogicalNot node.
*
* @internal
*
* @param node The node to visit.
*/
public String visit(LogicalNot node)
Expand All @@ -77,6 +83,8 @@ public String visit(LogicalNot node)
/**
* Visit a Tag node.
*
* @internal
*
* @param node The node to visit.
*/
public String visit(Tag node)
Expand All @@ -87,6 +95,8 @@ public String visit(Tag node)
/**
* Visit a pattern node.
*
* @internal
*
* @param node The node to visit.
*/
public String visit(Pattern node)
Expand All @@ -103,6 +113,8 @@ public String visit(Pattern node)
/**
* Visit a PatternLiteral node.
*
* @internal
*
* @param node The node to visit.
*
* @throws RenderException if the literal string contains the wildcard character.
Expand All @@ -125,6 +137,8 @@ public String visit(PatternLiteral node)
/**
* Visit a PatternWildcard node.
*
* @internal
*
* @param node The node to visit.
*/
public String visit(PatternWildcard node)
Expand All @@ -135,6 +149,8 @@ public String visit(PatternWildcard node)
/**
* Visit a EmptyExpression node.
*
* @internal
*
* @param node The node to visit.
*/
public String visit(EmptyExpression node)
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/co/icecave/dialekt/renderer/RendererInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.icecave.dialekt.renderer;

import co.icecave.dialekt.ast.ExpressionInterface;

public interface RendererInterface
{
/**
* Render an expression to a string.
*
* @param expression The expression to render.
*
* @return The rendered expression.
*/
public String render(ExpressionInterface expression);
}
Loading

0 comments on commit 9d0908b

Please sign in to comment.