summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/syntax/IterationRule.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io/devnulllabs/openjava/syntax/IterationRule.java')
-rw-r--r--src/main/java/io/devnulllabs/openjava/syntax/IterationRule.java78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/main/java/io/devnulllabs/openjava/syntax/IterationRule.java b/src/main/java/io/devnulllabs/openjava/syntax/IterationRule.java
new file mode 100644
index 0000000..5c7f75f
--- /dev/null
+++ b/src/main/java/io/devnulllabs/openjava/syntax/IterationRule.java
@@ -0,0 +1,78 @@
+/*
+ * IterationRule.java
+ *
+ * comments here.
+ *
+ * @author Michiaki Tatsubori
+ * @version %VERSION% %DATE%
+ * @see java.lang.Object
+ *
+ * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED.
+ */
+package io.devnulllabs.openjava.syntax;
+
+
+import io.devnulllabs.openjava.ptree.ObjectList;
+import io.devnulllabs.openjava.ptree.ParseTree;
+
+
+/**
+ * The class <code>IterationRule</code> represents iterative syntax
+ * rule.
+ * <p>
+ * Suppose there's a syntax rule A. This class can represents
+ * the syntax ( A )* or ( A )+
+ *
+ * @author Michiaki Tatsubori
+ * @version 1.0
+ * @since $Id: IterationRule.java,v 1.2 2003/02/19 02:54:31 tatsubori Exp $
+ * @see java.lang.Object
+ */
+public class IterationRule extends AbstractSyntaxRule
+{
+ private SyntaxRule elementRule;
+ private boolean allowsEmpty;
+
+ /**
+ * Allocates a new rule representing iterations of a given rule.
+ *
+ * @param elementRule a rule to iterate
+ * @param allowsEmpty a flag to allow 0 iteration if it is true.
+ */
+ public IterationRule( SyntaxRule elementRule, boolean allowsEmpty ) {
+ this.elementRule = elementRule;
+ this.allowsEmpty = allowsEmpty;
+ }
+
+ /**
+ * Allocates a new rule representing iterations of a given rule
+ * not allowing 0 iteration.
+ *
+ * @param elementRule a rule to iterate
+ */
+ public IterationRule( SyntaxRule elementRule ) {
+ this( elementRule, false );
+ }
+
+ /**
+ * Consumes token source.
+ *
+ * @param token_src token source.
+ * @return null if this fails to consume a syntax tree represented
+ * by this object. Otherwise it returns <code>ObjectList</code> object.
+ */
+ public ParseTree consume( TokenSource token_src )
+ throws SyntaxException
+ {
+ ObjectList result = new ObjectList();
+ if (! allowsEmpty) {
+ result.add( elementRule.consume( token_src ) );
+ }
+ while (elementRule.lookahead( token_src )) {
+ ParseTree elem = elementRule.consume( token_src );
+ result.add( elem );
+ }
+ return result;
+ }
+
+}