Ensure all files use Unix line endings
diff --git a/daffodil-cli-unittest/src/test/scala-new/edu/illinois/ncsa/daffodil/MainTests.scala b/daffodil-cli-unittest/src/test/scala-new/edu/illinois/ncsa/daffodil/MainTests.scala
index 5a81e6b..a9000f8 100644
--- a/daffodil-cli-unittest/src/test/scala-new/edu/illinois/ncsa/daffodil/MainTests.scala
+++ b/daffodil-cli-unittest/src/test/scala-new/edu/illinois/ncsa/daffodil/MainTests.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil

-

+package edu.illinois.ncsa.daffodil
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,156 +30,156 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import org.junit.Rule

-import edu.illinois.ncsa.daffodil.util.Misc

-import java.io.ByteArrayInputStream

-import org.junit.contrib.java.lang.system.ExpectedSystemExit

-import org.junit.contrib.java.lang.system.internal.CheckExitCalled

-

-class MainTests {

-

-  // This rule makes it so that System.exit throws an exception You are

-  // supposed to be able to say exit.expectExitWithStatus(), but this doesn't

-  // work, probably some scala problem, related to def exit. This is supposed

-  // to be val exit, but it doesn't work with scala. Instead, we need to

-  // manually catch CheckExitCalled and verify the status

-  @Rule

-  def exit = ExpectedSystemExit.none()

-

-  @Test def test_required_mode() {

-    try {

-      Main.main(Array(""))

-    } catch {

-      case c: CheckExitCalled => assertEquals(c.getStatus, 1)

-    }

-  }

-

-  @Test def test_require_schema_or_parser() {

-    try {

-      Main.main(Array("parse"))

-    } catch {

-      case c: CheckExitCalled => assertEquals(c.getStatus, 1)

-    }

-  }

-

-  val testDir = "/test/cli/"

-  val cli_choice = testDir + "choice.xsd"

-

-  @Test def test_main_parse_simple() {

-    try {

-      val schema = Misc.getRequiredResource(cli_choice).getPath

-      val data = "1;2;3;4;5;6;7;8"

-      val is = new ByteArrayInputStream(data.getBytes())

-      val oldSysin = System.in

-      System.setIn(is)

-

-      Main.main(Array("parse", "--schema", schema, "-"))

-

-      System.setIn(oldSysin)

-      fail()

-    } catch {

-      case c: CheckExitCalled => assertEquals(c.getStatus, 0)

-    }

-  }

-

-  @Test def test_main_parse_all_opts() {

-    try {

-      val schema = Misc.getRequiredResource(cli_choice).getPath

-      val data = "1;2;3;4.4;5;6;7;8"

-      val is = new ByteArrayInputStream(data.getBytes())

-      val oldSysin = System.in

-      System.setIn(is)

-

-      Main.main(Array("parse", "--schema", schema,

-        "--root", "ch1",

-        "--namespace", "http://www.example.org/example1/",

-        "-"))

-      System.setIn(oldSysin)

-      fail()

-    } catch {

-      case c: CheckExitCalled => assertEquals(c.getStatus, 0)

-    }

-  }

-

-  val cli_fixedLength = testDir + "fixedLength.dfdl.xsd"

-

-  @Test def test_main_left_over_data_NOT() {

-    try {

-      val schema = Misc.getRequiredResource(cli_fixedLength).getPath

-      val data = "1"

-      val is = new ByteArrayInputStream(data.getBytes())

-      val oldSysin = System.in

-      System.setIn(is)

-

-      Main.main(Array("parse", "--schema", schema,

-        "--root", "fixedLength",

-        "--namespace", "http://www.example.org/example1/",

-        "-"))

-      System.setIn(oldSysin)

-      fail()

-    } catch {

-      case c: CheckExitCalled => assertEquals(c.getStatus, 0)

-    }

-  }

-

-  @Test def test_main_left_over_data() {

-    try {

-      val schema = Misc.getRequiredResource(cli_fixedLength).getPath

-      val data = "1This is left over data"

-      val is = new ByteArrayInputStream(data.getBytes())

-      val oldSysin = System.in

-      System.setIn(is)

-

-      Main.main(Array("parse", "--schema", schema,

-        "--root", "fixedLength",

-        "--namespace", "http://www.example.org/example1/",

-        "-"))

-      System.setIn(oldSysin)

-      fail()

-    } catch {

-      case c: CheckExitCalled => assertEquals(c.getStatus, 1)

-    }

-  }

-

-  @Test def test_main_left_over_data2() {

-    try {

-      val schema = Misc.getRequiredResource(cli_fixedLength).getPath

-      val data = "This is real data.ENDThis is left over data"

-      val is = new ByteArrayInputStream(data.getBytes())

-      val oldSysin = System.in

-      System.setIn(is)

-

-      Main.main(Array("parse", "--schema", schema,

-        "--root", "delimLength",

-        "--namespace", "http://www.example.org/example1/",

-        "-"))

-      System.setIn(oldSysin)

-      fail()

-    } catch {

-      case c: CheckExitCalled => assertEquals(c.getStatus, 1)

-    }

-  }

-

-  //  val cli_unboundedBinary= testDir + "unboundedBinary.dfdl.xsd"

-  //

-  //  @Test def test_main_parse_unboundedBinary() {

-  //    try {

-  //      val schema = Misc.getRequiredResource(cli_unboundedBinary).getPath

-  //      val data = "abab4545babab73645454756565743"

-  //      val is = new ByteArrayInputStream(data.getBytes())

-  //      val oldSysin = System.in

-  //      System.setIn(is)

-  //

-  //      Main.main(Array("parse", "-s", schema, "-"))

-  //

-  //      System.setIn(oldSysin)

-  //    } catch {

-  //      case c: CheckExitCalled => assertEquals(c.getStatus, 0)

-  //    }

-  //  }

-

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import org.junit.Rule
+import edu.illinois.ncsa.daffodil.util.Misc
+import java.io.ByteArrayInputStream
+import org.junit.contrib.java.lang.system.ExpectedSystemExit
+import org.junit.contrib.java.lang.system.internal.CheckExitCalled
+
+class MainTests {
+
+  // This rule makes it so that System.exit throws an exception You are
+  // supposed to be able to say exit.expectExitWithStatus(), but this doesn't
+  // work, probably some scala problem, related to def exit. This is supposed
+  // to be val exit, but it doesn't work with scala. Instead, we need to
+  // manually catch CheckExitCalled and verify the status
+  @Rule
+  def exit = ExpectedSystemExit.none()
+
+  @Test def test_required_mode() {
+    try {
+      Main.main(Array(""))
+    } catch {
+      case c: CheckExitCalled => assertEquals(c.getStatus, 1)
+    }
+  }
+
+  @Test def test_require_schema_or_parser() {
+    try {
+      Main.main(Array("parse"))
+    } catch {
+      case c: CheckExitCalled => assertEquals(c.getStatus, 1)
+    }
+  }
+
+  val testDir = "/test/cli/"
+  val cli_choice = testDir + "choice.xsd"
+
+  @Test def test_main_parse_simple() {
+    try {
+      val schema = Misc.getRequiredResource(cli_choice).getPath
+      val data = "1;2;3;4;5;6;7;8"
+      val is = new ByteArrayInputStream(data.getBytes())
+      val oldSysin = System.in
+      System.setIn(is)
+
+      Main.main(Array("parse", "--schema", schema, "-"))
+
+      System.setIn(oldSysin)
+      fail()
+    } catch {
+      case c: CheckExitCalled => assertEquals(c.getStatus, 0)
+    }
+  }
+
+  @Test def test_main_parse_all_opts() {
+    try {
+      val schema = Misc.getRequiredResource(cli_choice).getPath
+      val data = "1;2;3;4.4;5;6;7;8"
+      val is = new ByteArrayInputStream(data.getBytes())
+      val oldSysin = System.in
+      System.setIn(is)
+
+      Main.main(Array("parse", "--schema", schema,
+        "--root", "ch1",
+        "--namespace", "http://www.example.org/example1/",
+        "-"))
+      System.setIn(oldSysin)
+      fail()
+    } catch {
+      case c: CheckExitCalled => assertEquals(c.getStatus, 0)
+    }
+  }
+
+  val cli_fixedLength = testDir + "fixedLength.dfdl.xsd"
+
+  @Test def test_main_left_over_data_NOT() {
+    try {
+      val schema = Misc.getRequiredResource(cli_fixedLength).getPath
+      val data = "1"
+      val is = new ByteArrayInputStream(data.getBytes())
+      val oldSysin = System.in
+      System.setIn(is)
+
+      Main.main(Array("parse", "--schema", schema,
+        "--root", "fixedLength",
+        "--namespace", "http://www.example.org/example1/",
+        "-"))
+      System.setIn(oldSysin)
+      fail()
+    } catch {
+      case c: CheckExitCalled => assertEquals(c.getStatus, 0)
+    }
+  }
+
+  @Test def test_main_left_over_data() {
+    try {
+      val schema = Misc.getRequiredResource(cli_fixedLength).getPath
+      val data = "1This is left over data"
+      val is = new ByteArrayInputStream(data.getBytes())
+      val oldSysin = System.in
+      System.setIn(is)
+
+      Main.main(Array("parse", "--schema", schema,
+        "--root", "fixedLength",
+        "--namespace", "http://www.example.org/example1/",
+        "-"))
+      System.setIn(oldSysin)
+      fail()
+    } catch {
+      case c: CheckExitCalled => assertEquals(c.getStatus, 1)
+    }
+  }
+
+  @Test def test_main_left_over_data2() {
+    try {
+      val schema = Misc.getRequiredResource(cli_fixedLength).getPath
+      val data = "This is real data.ENDThis is left over data"
+      val is = new ByteArrayInputStream(data.getBytes())
+      val oldSysin = System.in
+      System.setIn(is)
+
+      Main.main(Array("parse", "--schema", schema,
+        "--root", "delimLength",
+        "--namespace", "http://www.example.org/example1/",
+        "-"))
+      System.setIn(oldSysin)
+      fail()
+    } catch {
+      case c: CheckExitCalled => assertEquals(c.getStatus, 1)
+    }
+  }
+
+  //  val cli_unboundedBinary= testDir + "unboundedBinary.dfdl.xsd"
+  //
+  //  @Test def test_main_parse_unboundedBinary() {
+  //    try {
+  //      val schema = Misc.getRequiredResource(cli_unboundedBinary).getPath
+  //      val data = "abab4545babab73645454756565743"
+  //      val is = new ByteArrayInputStream(data.getBytes())
+  //      val oldSysin = System.in
+  //      System.setIn(is)
+  //
+  //      Main.main(Array("parse", "-s", schema, "-"))
+  //
+  //      System.setIn(oldSysin)
+  //    } catch {
+  //      case c: CheckExitCalled => assertEquals(c.getStatus, 0)
+  //    }
+  //  }
+
+}
diff --git a/daffodil-core-unittest/src/test/scala-new/edu/illinois/ncsa/daffodil/dsom/TestDsomCompilerNew.scala b/daffodil-core-unittest/src/test/scala-new/edu/illinois/ncsa/daffodil/dsom/TestDsomCompilerNew.scala
index 43635a4..13d277c 100644
--- a/daffodil-core-unittest/src/test/scala-new/edu/illinois/ncsa/daffodil/dsom/TestDsomCompilerNew.scala
+++ b/daffodil-core-unittest/src/test/scala-new/edu/illinois/ncsa/daffodil/dsom/TestDsomCompilerNew.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.dsom

+package edu.illinois.ncsa.daffodil.dsom
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,104 +32,104 @@
  * SOFTWARE.
  */
 
-

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.util._

-import scala.xml._

-import edu.illinois.ncsa.daffodil.compiler._

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen._

-import edu.illinois.ncsa.daffodil.schema.annotation.props._

-import edu.illinois.ncsa.daffodil.util.Misc

-import junit.framework.Assert.assertEquals

-import junit.framework.Assert.assertTrue

-import java.io.FileOutputStream

-import java.nio.channels.WritableByteChannel

-import java.io.FileWriter

-import java.io.File

-import java.nio.ByteBuffer

-import org.junit.Test

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-

-class TestDsomCompilerNew extends Logging {

-  val xsd = XMLUtils.XSD_NAMESPACE

-  val dfdl = XMLUtils.DFDL_NAMESPACE

-  val xsi = XMLUtils.XSI_NAMESPACE

-  val example = XMLUtils.EXAMPLE_NAMESPACE

-

-  val dummyGroupRef = null // just because otherwise we have to construct too many things.

-

-  def FindValue(collection: Map[String, String], key: String, value: String): Boolean = {

-    val found: Boolean = Option(collection.find(x => x._1 == key && x._2 == value)) match {

-      case Some(_) => true

-      case None => false

-    }

-    found

-  }

-

-  @Test def testHasPatternFacets() {

-    val testSchema = TestUtils.dfdlTestSchema(

-      <dfdl:format ref="tns:daffodilTest1"/>,

-      <xs:element name="e1" type="tns:st1" dfdl:lengthKind="explicit" dfdl:length="1" />

-      <xs:simpleType name="st1">

-        <xs:restriction base="xs:string">

-          <xs:pattern value="1"/>

-          <xs:pattern value="2"/>

-          <xs:pattern value="3"/>

-        </xs:restriction>

-      </xs:simpleType>)

-

-    val compiler = Compiler()

-    val (sset, _) = compiler.frontEnd(testSchema)

-    val Seq(schema) = sset.schemas

-    val Seq(schemaDoc) = schema.schemaDocuments

-    val Seq(declf) = schemaDoc.globalElementDecls

-    val decl = declf.forRoot()

-    

-    assertEquals(1, decl.patternValues.length)

-    val (facetName, pattern) = decl.patternValues(0)

-    assertEquals("1|2|3", pattern.toString())

-  }

-

-  @Test def testPatternFacetsInheritance() {

-    val testSchema = TestUtils.dfdlTestSchema(

-      <dfdl:format ref="tns:daffodilTest1"/>,

-      <xs:element name="e1" type="tns:st1" dfdl:lengthKind="explicit" dfdl:length="1"/>

-      <xs:simpleType name="st1">

-        <xs:restriction base="tns:st2">

-          <xs:pattern value="1"/>

-          <xs:pattern value="2"/>

-          <xs:pattern value="3"/>

-        </xs:restriction>

-      </xs:simpleType>

-      <xs:simpleType name="st2">

-        <xs:restriction base="tns:st3">

-          <xs:pattern value="4"/>

-          <xs:pattern value="5"/>

-          <xs:pattern value="6"/>

-        </xs:restriction>

-      </xs:simpleType>

-      <xs:simpleType name="st3">

-        <xs:restriction base="xs:string">

-          <xs:pattern value="7"/>

-          <xs:pattern value="8"/>

-          <xs:pattern value="9"/>

-        </xs:restriction>

-      </xs:simpleType>)

-

-    val compiler = Compiler()

-    val (sset, _) = compiler.frontEnd(testSchema)

-    val Seq(schema) = sset.schemas

-    val Seq(schemaDoc) = schema.schemaDocuments

-    val Seq(declf) = schemaDoc.globalElementDecls

-    val decl = declf.forRoot()

-

-    assertEquals(3, decl.patternValues.length)

-    val (_, st1) = decl.patternValues(0)

-    val (_, st2) = decl.patternValues(1)

-    val (_, st3) = decl.patternValues(2)

-    

-    assertEquals("1|2|3", st1.toString())

-    assertEquals("4|5|6", st2.toString())

-    assertEquals("7|8|9", st3.toString())

-  }

+
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.util._
+import scala.xml._
+import edu.illinois.ncsa.daffodil.compiler._
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen._
+import edu.illinois.ncsa.daffodil.schema.annotation.props._
+import edu.illinois.ncsa.daffodil.util.Misc
+import junit.framework.Assert.assertEquals
+import junit.framework.Assert.assertTrue
+import java.io.FileOutputStream
+import java.nio.channels.WritableByteChannel
+import java.io.FileWriter
+import java.io.File
+import java.nio.ByteBuffer
+import org.junit.Test
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+
+class TestDsomCompilerNew extends Logging {
+  val xsd = XMLUtils.XSD_NAMESPACE
+  val dfdl = XMLUtils.DFDL_NAMESPACE
+  val xsi = XMLUtils.XSI_NAMESPACE
+  val example = XMLUtils.EXAMPLE_NAMESPACE
+
+  val dummyGroupRef = null // just because otherwise we have to construct too many things.
+
+  def FindValue(collection: Map[String, String], key: String, value: String): Boolean = {
+    val found: Boolean = Option(collection.find(x => x._1 == key && x._2 == value)) match {
+      case Some(_) => true
+      case None => false
+    }
+    found
+  }
+
+  @Test def testHasPatternFacets() {
+    val testSchema = TestUtils.dfdlTestSchema(
+      <dfdl:format ref="tns:daffodilTest1"/>,
+      <xs:element name="e1" type="tns:st1" dfdl:lengthKind="explicit" dfdl:length="1" />
+      <xs:simpleType name="st1">
+        <xs:restriction base="xs:string">
+          <xs:pattern value="1"/>
+          <xs:pattern value="2"/>
+          <xs:pattern value="3"/>
+        </xs:restriction>
+      </xs:simpleType>)
+
+    val compiler = Compiler()
+    val (sset, _) = compiler.frontEnd(testSchema)
+    val Seq(schema) = sset.schemas
+    val Seq(schemaDoc) = schema.schemaDocuments
+    val Seq(declf) = schemaDoc.globalElementDecls
+    val decl = declf.forRoot()
+    
+    assertEquals(1, decl.patternValues.length)
+    val (facetName, pattern) = decl.patternValues(0)
+    assertEquals("1|2|3", pattern.toString())
+  }
+
+  @Test def testPatternFacetsInheritance() {
+    val testSchema = TestUtils.dfdlTestSchema(
+      <dfdl:format ref="tns:daffodilTest1"/>,
+      <xs:element name="e1" type="tns:st1" dfdl:lengthKind="explicit" dfdl:length="1"/>
+      <xs:simpleType name="st1">
+        <xs:restriction base="tns:st2">
+          <xs:pattern value="1"/>
+          <xs:pattern value="2"/>
+          <xs:pattern value="3"/>
+        </xs:restriction>
+      </xs:simpleType>
+      <xs:simpleType name="st2">
+        <xs:restriction base="tns:st3">
+          <xs:pattern value="4"/>
+          <xs:pattern value="5"/>
+          <xs:pattern value="6"/>
+        </xs:restriction>
+      </xs:simpleType>
+      <xs:simpleType name="st3">
+        <xs:restriction base="xs:string">
+          <xs:pattern value="7"/>
+          <xs:pattern value="8"/>
+          <xs:pattern value="9"/>
+        </xs:restriction>
+      </xs:simpleType>)
+
+    val compiler = Compiler()
+    val (sset, _) = compiler.frontEnd(testSchema)
+    val Seq(schema) = sset.schemas
+    val Seq(schemaDoc) = schema.schemaDocuments
+    val Seq(declf) = schemaDoc.globalElementDecls
+    val decl = declf.forRoot()
+
+    assertEquals(3, decl.patternValues.length)
+    val (_, st1) = decl.patternValues(0)
+    val (_, st2) = decl.patternValues(1)
+    val (_, st3) = decl.patternValues(2)
+    
+    assertEquals("1|2|3", st1.toString())
+    assertEquals("4|5|6", st2.toString())
+    assertEquals("7|8|9", st3.toString())
+  }
 }
diff --git a/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/dsom/TestEntityReplacer.scala b/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/dsom/TestEntityReplacer.scala
index f3e3ae7..da5b83c 100644
--- a/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/dsom/TestEntityReplacer.scala
+++ b/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/dsom/TestEntityReplacer.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.dsom

-

+package edu.illinois.ncsa.daffodil.dsom
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,161 +30,161 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert.assertEquals

-import junit.framework.Assert.assertTrue

-import java.util.regex.Pattern

-import java.util.regex.Matcher

-import org.junit.Test

-import edu.illinois.ncsa.daffodil.Implicits._

-

-class TestEntityReplacer {

-

-  @Test def testEmptyString = {

-    val e = new EntityReplacer

-    assertEquals(e.replaceAll(""), "")

-  }

-

-  @Test def testEscapeScheme = {

-    val f1 = intercept[Exception] { EntityReplacer.replaceAll("Text%Text") }

-    assertEquals("Invalid DFDL Entity (%Text) found in \"Text%Text\"", f1.getMessage()) // Works basic case

-    assertEquals("Text%Text", EntityReplacer.replaceAll("Text%%Text")) // Works basic case

-    assertEquals("Text%#%Text", EntityReplacer.replaceAll("Text%%#%%Text")) // Works multiple

-

-    val f2 = intercept[Exception] { EntityReplacer.replaceAll("Text%%%#%%Text") }

-    assertEquals("Invalid DFDL Entity (%#) found in \"%#\"", f2.getMessage) // Works multiple

-    assertEquals("Text%AText", EntityReplacer.replaceAll("Text%%%#65;Text")) // Works multiple

-  }

-

-  @Test def testEntityReplacement = {

-    assertEquals("Text\u0000Text", EntityReplacer.replaceAll("Text%NUL;Text")) // Works basic case

-

-    val f1 = intercept[Exception] { EntityReplacer.replaceAll("Text%NUL;%NULText") }

-    assertEquals("Invalid DFDL Entity (%NULText) found in \"Text%NUL;%NULText\"", f1.getMessage) // Works basic case

-    assertEquals("Text\u0000\u0007Text", EntityReplacer.replaceAll("Text%NUL;%BEL;Text")) // Works basic case

-  }

-

-  @Test def testHexadecimalCodePointReplacement = {

-    assertEquals("Text\u0000Text", EntityReplacer.replaceAll("Text%#x0000;Text")) // Works basic case

-    assertEquals("Text\u0000Text\u000D", EntityReplacer.replaceAll("Text%#x0000;Text%#x000D;")) // Works multiple hex

-

-    val f1 = intercept[Exception] { EntityReplacer.replaceAll("Text%#x0000;Text%#x000D") }

-    assertEquals("Invalid DFDL Entity (%#x000D) found in \"Text%#x0000;Text%#x000D\"", f1.getMessage()) // Works one proper, one improper

-  }

-

-  @Test def testDecimalCodePointReplacement = {

-    assertEquals("TextAText", EntityReplacer.replaceAll("Text%#65;Text")) // Works basic case

-    assertEquals("TextAText", EntityReplacer.replaceAll("Text%#0000000000065;Text")) // Works basic case w/ padding

-    assertEquals("TextATextB", EntityReplacer.replaceAll("Text%#65;Text%#66;")) // Works multiple

-    assertEquals("TextATextB", EntityReplacer.replaceAll("Text%#65;Text%#000000000066;")) // Works multiple w/ padding

-

-    val f1 = intercept[Exception] { EntityReplacer.replaceAll("Text%#65;Text%#66") }

-    assertEquals("Invalid DFDL Entity (%#66) found in \"Text%#65;Text%#66\"", f1.getMessage) // Works one proper, one improper

-  }

-

-  @Test def testRawByteReplacement = {

-    assertEquals("ÿ", EntityReplacer.replaceAll("%#rFF;"))

-    assertEquals("ÿ ú", EntityReplacer.replaceAll("%#rFF; %#rFA;"))

-

-    val f1 = intercept[Exception] { EntityReplacer.replaceAll("%#rFF; %#rFA") }

-    assertEquals("Invalid DFDL Entity (%#rFA) found in \"%#rFF; %#rFA\"", f1.getMessage)

-  }

-

-  @Test def testInvalidDfdlEntities = {

-    val f1 = intercept[Exception] { EntityReplacer.replaceAll("%#rTT;") }

-    assertEquals("Invalid DFDL Entity (%#rTT;) found in \"%#rTT;\"", f1.getMessage) // Verify fails: Raw Byte

-

-    val f2 = intercept[Exception] { EntityReplacer.replaceAll("%#A;") }

-    assertEquals("Invalid DFDL Entity (%#A;) found in \"%#A;\"", f2.getMessage) // Verify fails: Decimal Code Point

-

-    val f3 = intercept[Exception] { EntityReplacer.replaceAll("%#xTT;") }

-    assertEquals("Invalid DFDL Entity (%#xTT;) found in \"%#xTT;\"", f3.getMessage) // Verify fails: Hexadecimal Code Point

-

-    val f4 = intercept[Exception] { EntityReplacer.replaceAll("%SomeInvalidName;") }

-    assertEquals("Invalid DFDL Entity (%SomeInvalidName;) found in \"%SomeInvalidName;\"", f4.getMessage) // Verify fails: Dfdl Entity

-  }

-

-  @Test def testAll = {

-    val e = new EntityReplacer

-    val testString = new StringBuilder

-    testString.append("Text#%%Text")

-    testString.append("Text%#x0000;Text%%#x000D")

-    testString.append("Text%#65;Text")

-    testString.append("%#rFF; %#rFA;")

-    testString.append("Text%NUL;%BEL;Text")

-    testString.append("Text%NL;Text")

-

-    val solutionString = new StringBuilder

-    solutionString.append("Text#%Text")

-    solutionString.append("Text\u0000Text%#x000D")

-    solutionString.append("TextAText")

-    solutionString.append("ÿ ú")

-    solutionString.append("Text\u0000\u0007Text")

-    solutionString.append("Text%NL;Text")

-

-    val resultString = e.replaceAll(testString.toString())

-

-    assertEquals(solutionString.toString(), resultString)

-  }

-

-  @Test def testMultiplePercents = {

-    val testString1 = "Text1%%%%%%%%%%Text2"

-    val solution1 = "Text1%%%%%Text2"

-    val testString2 = "%Text1%%%%%%%%%%Text2"

-    val testString3 = "Text1%%%%%%%%%%Text2%"

-    val testString4 = "%Text1%%%%%%%%%%Text2%"

-    val testString5 = "%%Text1%%%%%%%%%%Text2%%"

-    val solution5 = "%Text1%%%%%Text2%"

-    val testString6 = "%Text1%%%%%%%%%%Text2%%"

-    val testString7 = "%%Text1%%%%%%%%%%Text2%"

-    val testString8 = "%%Text1%%%%%%%%%%Text2"

-    val solution8 = "%Text1%%%%%Text2"

-    val testString9 = "Text1%%%%%%%%%%Text2%%"

-    val solution9 = "Text1%%%%%Text2%"

-

-    assertEquals(solution1, EntityReplacer.replaceAll(testString1, None))

-

-    val f1 = intercept[Exception] { EntityReplacer.replaceAll(testString2, None) }

-    assertEquals("Invalid DFDL Entity (%Text1) found in \"%Text1\"", f1.getMessage)

-

-    val f2 = intercept[Exception] { EntityReplacer.replaceAll(testString3, None) }

-    assertEquals("Invalid DFDL Entity (%) found in \"Text2%\"", f2.getMessage)

-

-    val f3 = intercept[Exception] { EntityReplacer.replaceAll(testString4) }

-    assertEquals("Invalid DFDL Entity (%Text1) found in \"%Text1\"", f3.getMessage)

-

-    assertEquals(solution5, EntityReplacer.replaceAll(testString5, None))

-

-    val f4 = intercept[Exception] { EntityReplacer.replaceAll(testString6) }

-    assertEquals("Invalid DFDL Entity (%Text1) found in \"%Text1\"", f4.getMessage)

-

-    val f5 = intercept[Exception] { EntityReplacer.replaceAll(testString7) }

-    assertEquals("Invalid DFDL Entity (%) found in \"Text2%\"", f5.getMessage)

-

-    assertEquals(solution8, EntityReplacer.replaceAll(testString8, None))

-    assertEquals(solution9, EntityReplacer.replaceAll(testString9, None))

-  }

-

-  @Test def testMultipleSemicolons = {

-    val testString1 = "Text1;;;;;;;;;;Text2"

-    val testString2 = ";Text1;;;;;;;;;;Text2"

-    val testString3 = "Text1;;;;;;;;;;Text2;"

-    val testString4 = ";Text1;;;;;;;;;;Text2;"

-    val testString5 = ";;Text1;;;;;;;;;;Text2;;"

-    val testString6 = ";Text1;;;;;;;;;;Text2;;"

-    val testString7 = ";;Text1;;;;;;;;;;Text2;"

-    val testString8 = ";;Text1;;;;;;;;;;Text2"

-    val testString9 = "Text1;;;;;;;;;;Text2;;"

-    assertEquals(testString1, EntityReplacer.replaceAll(testString1, None))

-    assertEquals(testString2, EntityReplacer.replaceAll(testString2, None))

-    assertEquals(testString3, EntityReplacer.replaceAll(testString3, None))

-    assertEquals(testString4, EntityReplacer.replaceAll(testString4, None))

-    assertEquals(testString5, EntityReplacer.replaceAll(testString5, None))

-    assertEquals(testString6, EntityReplacer.replaceAll(testString6, None))

-    assertEquals(testString7, EntityReplacer.replaceAll(testString7, None))

-    assertEquals(testString8, EntityReplacer.replaceAll(testString8, None))

-    assertEquals(testString9, EntityReplacer.replaceAll(testString9, None))

-  }

-

-}

+ */
+
+import junit.framework.Assert.assertEquals
+import junit.framework.Assert.assertTrue
+import java.util.regex.Pattern
+import java.util.regex.Matcher
+import org.junit.Test
+import edu.illinois.ncsa.daffodil.Implicits._
+
+class TestEntityReplacer {
+
+  @Test def testEmptyString = {
+    val e = new EntityReplacer
+    assertEquals(e.replaceAll(""), "")
+  }
+
+  @Test def testEscapeScheme = {
+    val f1 = intercept[Exception] { EntityReplacer.replaceAll("Text%Text") }
+    assertEquals("Invalid DFDL Entity (%Text) found in \"Text%Text\"", f1.getMessage()) // Works basic case
+    assertEquals("Text%Text", EntityReplacer.replaceAll("Text%%Text")) // Works basic case
+    assertEquals("Text%#%Text", EntityReplacer.replaceAll("Text%%#%%Text")) // Works multiple
+
+    val f2 = intercept[Exception] { EntityReplacer.replaceAll("Text%%%#%%Text") }
+    assertEquals("Invalid DFDL Entity (%#) found in \"%#\"", f2.getMessage) // Works multiple
+    assertEquals("Text%AText", EntityReplacer.replaceAll("Text%%%#65;Text")) // Works multiple
+  }
+
+  @Test def testEntityReplacement = {
+    assertEquals("Text\u0000Text", EntityReplacer.replaceAll("Text%NUL;Text")) // Works basic case
+
+    val f1 = intercept[Exception] { EntityReplacer.replaceAll("Text%NUL;%NULText") }
+    assertEquals("Invalid DFDL Entity (%NULText) found in \"Text%NUL;%NULText\"", f1.getMessage) // Works basic case
+    assertEquals("Text\u0000\u0007Text", EntityReplacer.replaceAll("Text%NUL;%BEL;Text")) // Works basic case
+  }
+
+  @Test def testHexadecimalCodePointReplacement = {
+    assertEquals("Text\u0000Text", EntityReplacer.replaceAll("Text%#x0000;Text")) // Works basic case
+    assertEquals("Text\u0000Text\u000D", EntityReplacer.replaceAll("Text%#x0000;Text%#x000D;")) // Works multiple hex
+
+    val f1 = intercept[Exception] { EntityReplacer.replaceAll("Text%#x0000;Text%#x000D") }
+    assertEquals("Invalid DFDL Entity (%#x000D) found in \"Text%#x0000;Text%#x000D\"", f1.getMessage()) // Works one proper, one improper
+  }
+
+  @Test def testDecimalCodePointReplacement = {
+    assertEquals("TextAText", EntityReplacer.replaceAll("Text%#65;Text")) // Works basic case
+    assertEquals("TextAText", EntityReplacer.replaceAll("Text%#0000000000065;Text")) // Works basic case w/ padding
+    assertEquals("TextATextB", EntityReplacer.replaceAll("Text%#65;Text%#66;")) // Works multiple
+    assertEquals("TextATextB", EntityReplacer.replaceAll("Text%#65;Text%#000000000066;")) // Works multiple w/ padding
+
+    val f1 = intercept[Exception] { EntityReplacer.replaceAll("Text%#65;Text%#66") }
+    assertEquals("Invalid DFDL Entity (%#66) found in \"Text%#65;Text%#66\"", f1.getMessage) // Works one proper, one improper
+  }
+
+  @Test def testRawByteReplacement = {
+    assertEquals("ÿ", EntityReplacer.replaceAll("%#rFF;"))
+    assertEquals("ÿ ú", EntityReplacer.replaceAll("%#rFF; %#rFA;"))
+
+    val f1 = intercept[Exception] { EntityReplacer.replaceAll("%#rFF; %#rFA") }
+    assertEquals("Invalid DFDL Entity (%#rFA) found in \"%#rFF; %#rFA\"", f1.getMessage)
+  }
+
+  @Test def testInvalidDfdlEntities = {
+    val f1 = intercept[Exception] { EntityReplacer.replaceAll("%#rTT;") }
+    assertEquals("Invalid DFDL Entity (%#rTT;) found in \"%#rTT;\"", f1.getMessage) // Verify fails: Raw Byte
+
+    val f2 = intercept[Exception] { EntityReplacer.replaceAll("%#A;") }
+    assertEquals("Invalid DFDL Entity (%#A;) found in \"%#A;\"", f2.getMessage) // Verify fails: Decimal Code Point
+
+    val f3 = intercept[Exception] { EntityReplacer.replaceAll("%#xTT;") }
+    assertEquals("Invalid DFDL Entity (%#xTT;) found in \"%#xTT;\"", f3.getMessage) // Verify fails: Hexadecimal Code Point
+
+    val f4 = intercept[Exception] { EntityReplacer.replaceAll("%SomeInvalidName;") }
+    assertEquals("Invalid DFDL Entity (%SomeInvalidName;) found in \"%SomeInvalidName;\"", f4.getMessage) // Verify fails: Dfdl Entity
+  }
+
+  @Test def testAll = {
+    val e = new EntityReplacer
+    val testString = new StringBuilder
+    testString.append("Text#%%Text")
+    testString.append("Text%#x0000;Text%%#x000D")
+    testString.append("Text%#65;Text")
+    testString.append("%#rFF; %#rFA;")
+    testString.append("Text%NUL;%BEL;Text")
+    testString.append("Text%NL;Text")
+
+    val solutionString = new StringBuilder
+    solutionString.append("Text#%Text")
+    solutionString.append("Text\u0000Text%#x000D")
+    solutionString.append("TextAText")
+    solutionString.append("ÿ ú")
+    solutionString.append("Text\u0000\u0007Text")
+    solutionString.append("Text%NL;Text")
+
+    val resultString = e.replaceAll(testString.toString())
+
+    assertEquals(solutionString.toString(), resultString)
+  }
+
+  @Test def testMultiplePercents = {
+    val testString1 = "Text1%%%%%%%%%%Text2"
+    val solution1 = "Text1%%%%%Text2"
+    val testString2 = "%Text1%%%%%%%%%%Text2"
+    val testString3 = "Text1%%%%%%%%%%Text2%"
+    val testString4 = "%Text1%%%%%%%%%%Text2%"
+    val testString5 = "%%Text1%%%%%%%%%%Text2%%"
+    val solution5 = "%Text1%%%%%Text2%"
+    val testString6 = "%Text1%%%%%%%%%%Text2%%"
+    val testString7 = "%%Text1%%%%%%%%%%Text2%"
+    val testString8 = "%%Text1%%%%%%%%%%Text2"
+    val solution8 = "%Text1%%%%%Text2"
+    val testString9 = "Text1%%%%%%%%%%Text2%%"
+    val solution9 = "Text1%%%%%Text2%"
+
+    assertEquals(solution1, EntityReplacer.replaceAll(testString1, None))
+
+    val f1 = intercept[Exception] { EntityReplacer.replaceAll(testString2, None) }
+    assertEquals("Invalid DFDL Entity (%Text1) found in \"%Text1\"", f1.getMessage)
+
+    val f2 = intercept[Exception] { EntityReplacer.replaceAll(testString3, None) }
+    assertEquals("Invalid DFDL Entity (%) found in \"Text2%\"", f2.getMessage)
+
+    val f3 = intercept[Exception] { EntityReplacer.replaceAll(testString4) }
+    assertEquals("Invalid DFDL Entity (%Text1) found in \"%Text1\"", f3.getMessage)
+
+    assertEquals(solution5, EntityReplacer.replaceAll(testString5, None))
+
+    val f4 = intercept[Exception] { EntityReplacer.replaceAll(testString6) }
+    assertEquals("Invalid DFDL Entity (%Text1) found in \"%Text1\"", f4.getMessage)
+
+    val f5 = intercept[Exception] { EntityReplacer.replaceAll(testString7) }
+    assertEquals("Invalid DFDL Entity (%) found in \"Text2%\"", f5.getMessage)
+
+    assertEquals(solution8, EntityReplacer.replaceAll(testString8, None))
+    assertEquals(solution9, EntityReplacer.replaceAll(testString9, None))
+  }
+
+  @Test def testMultipleSemicolons = {
+    val testString1 = "Text1;;;;;;;;;;Text2"
+    val testString2 = ";Text1;;;;;;;;;;Text2"
+    val testString3 = "Text1;;;;;;;;;;Text2;"
+    val testString4 = ";Text1;;;;;;;;;;Text2;"
+    val testString5 = ";;Text1;;;;;;;;;;Text2;;"
+    val testString6 = ";Text1;;;;;;;;;;Text2;;"
+    val testString7 = ";;Text1;;;;;;;;;;Text2;"
+    val testString8 = ";;Text1;;;;;;;;;;Text2"
+    val testString9 = "Text1;;;;;;;;;;Text2;;"
+    assertEquals(testString1, EntityReplacer.replaceAll(testString1, None))
+    assertEquals(testString2, EntityReplacer.replaceAll(testString2, None))
+    assertEquals(testString3, EntityReplacer.replaceAll(testString3, None))
+    assertEquals(testString4, EntityReplacer.replaceAll(testString4, None))
+    assertEquals(testString5, EntityReplacer.replaceAll(testString5, None))
+    assertEquals(testString6, EntityReplacer.replaceAll(testString6, None))
+    assertEquals(testString7, EntityReplacer.replaceAll(testString7, None))
+    assertEquals(testString8, EntityReplacer.replaceAll(testString8, None))
+    assertEquals(testString9, EntityReplacer.replaceAll(testString9, None))
+  }
+
+}
diff --git a/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/parser/TestParsingBehaviors.scala b/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/parser/TestParsingBehaviors.scala
index 57d60d2..a97d26f 100644
--- a/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/parser/TestParsingBehaviors.scala
+++ b/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/parser/TestParsingBehaviors.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.parser

-

+package edu.illinois.ncsa.daffodil.parser
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,423 +30,423 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import scala.collection.mutable.Queue

-import java.util.regex.Pattern

-import java.io.FileInputStream

-import edu.illinois.ncsa.daffodil.util.Misc

-import javax.xml.transform.stream.StreamSource

-import java.io.File

-import java.nio.charset.Charset

-import java.net.URI

-import org.junit.Test

-import edu.illinois.ncsa.daffodil.dsom.Fakes

-import edu.illinois.ncsa.daffodil.processors.DFDLByteReader

-import edu.illinois.ncsa.daffodil.processors.TextJustificationType

-import edu.illinois.ncsa.daffodil.processors.DelimParser

-import edu.illinois.ncsa.daffodil.processors.DelimParseSuccess

-import edu.illinois.ncsa.daffodil.dsom.Fakes

-import edu.illinois.ncsa.daffodil.processors.DFDLUTStringReader

-

-class TestParsingBehaviors {

-

-  val testFileDir = "/test/"

-

-  val rsrcAB007 = Misc.getRequiredResource(testFileDir + "AB007.in")

-

-  @Test def testEscapeCharacterRemoval_Same = {

-    // escape and escapeEscape are the same

-    val input0 = "texttexttext"

-    val input1 = "text1//text2"

-    val input2 = "text1//text2//text3"

-    val input3 = "text1////text2"

-    val input4 = "//text1"

-    val input5 = "text1//"

-    val input6 = "//text1//text2"

-    val input7 = "text1//text2//"

-    val input8 = "text1/,text2"

-    val input9 = "text1///,text2"

-    val input10 = "/,text1"

-

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-

-    assertEquals("texttexttext", d.removeEscapeCharacters(input0, "/", "/", ","))

-    assertEquals("text1/text2", d.removeEscapeCharacters(input1, "/", "/", ","))

-    assertEquals("text1/text2/text3", d.removeEscapeCharacters(input2, "/", "/", ","))

-    assertEquals("text1//text2", d.removeEscapeCharacters(input3, "/", "/", ","))

-    assertEquals("/text1", d.removeEscapeCharacters(input4, "/", "/", ","))

-    assertEquals("text1/", d.removeEscapeCharacters(input5, "/", "/", ","))

-    assertEquals("/text1/text2", d.removeEscapeCharacters(input6, "/", "/", ","))

-    assertEquals("text1/text2/", d.removeEscapeCharacters(input7, "/", "/", ","))

-    assertEquals("text1,text2", d.removeEscapeCharacters(input8, "/", "/", ","))

-    assertEquals("text1/,text2", d.removeEscapeCharacters(input9, "/", "/", ","))

-    assertEquals(",text1", d.removeEscapeCharacters(input10, "/", "/", ","))

-  }

-

-  @Test def testEscapeCharacterRemoval_Diff = {

-    // different escape and escapeEscape characters

-    val input0 = "texttexttext"

-    val input1 = "text1%/text2"

-    val input2 = "text1%/text2%/text3"

-    val input3 = "text1%/%/text2"

-    val input4 = "%/text1"

-    val input5 = "text1%/"

-    val input6 = "%/text1%/text2"

-    val input7 = "text1%/text2%/"

-    val input8 = "text1/,text2"

-    val input9 = "text1%//,text2"

-    val input10 = "/,text1"

-    val input11 = "text1/?text2"

-    val input12 = "text1%text2"

-    val input13 = "text1%%/text2"

-    val input14 = "text1%/%text2"

-

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-

-    assertEquals("texttexttext", d.removeEscapeCharacters(input0, "%", "/", ","))

-    assertEquals("text1/text2", d.removeEscapeCharacters(input1, "%", "/", ","))

-    assertEquals("text1/text2/text3", d.removeEscapeCharacters(input2, "%", "/", ","))

-    assertEquals("text1//text2", d.removeEscapeCharacters(input3, "%", "/", ","))

-    assertEquals("/text1", d.removeEscapeCharacters(input4, "%", "/", ","))

-    assertEquals("text1/", d.removeEscapeCharacters(input5, "%", "/", ","))

-    assertEquals("/text1/text2", d.removeEscapeCharacters(input6, "%", "/", ","))

-    assertEquals("text1/text2/", d.removeEscapeCharacters(input7, "%", "/", ","))

-    assertEquals("text1,text2", d.removeEscapeCharacters(input8, "%", "/", ","))

-    assertEquals("text1/,text2", d.removeEscapeCharacters(input9, "%", "/", ","))

-    assertEquals(",text1", d.removeEscapeCharacters(input10, "%", "/", ","))

-    assertEquals("text1?text2", d.removeEscapeCharacters(input11, "%", "/", ","))

-    assertEquals("text1%text2", d.removeEscapeCharacters(input12, "%", "/", ","))

-    assertEquals("text1%/text2", d.removeEscapeCharacters(input13, "%", "/", ","))

-    assertEquals("text1/%text2", d.removeEscapeCharacters(input14, "%", "/", ","))

-  }

-

-  @Test def testEscapeCharacterRemoval_Diff_MultiCharDelim = {

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val input0 = "text1/septext2"

-    val input1 = "text1%//septext2"

-    val input2 = "/septext1text2"

-

-    assertEquals("text1septext2", d.removeEscapeCharacters(input0, "%", "/", "sep"))

-    assertEquals("text1/septext2", d.removeEscapeCharacters(input1, "%", "/", "sep"))

-    assertEquals("septext1text2", d.removeEscapeCharacters(input2, "%", "/", "sep"))

-  }

-

-  // Invalid, escapeBlockRemoval code expects valid start/end blocks already picked off

-  //  @Test def testEscapeBlockRemoval_Diff = {

-  //    // Different Start/End characters

-  //    val qInputOutput = Queue.empty[(String, String)]

-  //    val qOutput = Queue.empty[String]

-  //

-  //    qInputOutput.enqueue("texttext" -> "texttext")

-  //    qInputOutput.enqueue("[[texttext]" -> "[texttext")

-  //    qInputOutput.enqueue("]texttext" -> "]texttext")

-  //    qInputOutput.enqueue("text[text" -> "text[text")

-  //    qInputOutput.enqueue("text]text" -> "text]text")

-  //    qInputOutput.enqueue("texttext]" -> "texttext]")

-  //    qInputOutput.enqueue("[[[texttext]" -> "[[texttext")

-  //    qInputOutput.enqueue("texttext]]" -> "texttext]]")

-  //    qInputOutput.enqueue("text[[text" -> "text[[text")

-  //    qInputOutput.enqueue("text]]text" -> "text]]text")

-  //    qInputOutput.enqueue("[[texttext%]]" -> "[texttext]")

-  //    qInputOutput.enqueue("[[text%]text]" -> "[text]text")

-  //    qInputOutput.enqueue("text[text]" -> "text[text]")

-  //    qInputOutput.enqueue("[[text[text%]]" -> "[text[text]")

-  //    qInputOutput.enqueue("[[text%]text%]]" -> "[text]text]")

-  //    qInputOutput.enqueue("[[[texttext%]]" -> "[[texttext]")

-  //    qInputOutput.enqueue("[[texttext%]%]]" -> "[texttext]]")

-  //    qInputOutput.enqueue("[[[texttext%]%]]" -> "[[texttext]]")

-  //    qInputOutput.enqueue("text%text" -> "text%text")

-  //    qInputOutput.enqueue("text%%text" -> "text%%text")

-  //    qInputOutput.enqueue("text%[text" -> "text%[text")

-  //    qInputOutput.enqueue("text%]text" -> "text%]text")

-  //    qInputOutput.enqueue("%[texttext" -> "%[texttext")

-  //    qInputOutput.enqueue("texttext%]" -> "texttext%]")

-  //    qInputOutput.enqueue("%[texttext%]" -> "%[texttext%]")

-  //    qInputOutput.enqueue("[[text%text%]]" -> "[text%text]")

-  //    qInputOutput.enqueue("[[text%%]text%]]" -> "[text%]text]")

-  //    qInputOutput.enqueue("[text;text]" -> "text;text")

-  //    qInputOutput.enqueue("[text%;text]" -> "text%;text")

-  //    qInputOutput.enqueue("[[text;text%]]" -> "[text;text]")

-  //    qInputOutput.enqueue("[text?text]" -> "text?text")

-  //

-  //    val d = new DelimParser(Fakes.fakeElem)

-  //    var idx = 1

-  //    qInputOutput.foreach(x => {

-  //      println("trying... expect: " + x._2 + " for input: " + x._1)

-  //      val result = d.removeEscapesBlocks(x._1, "%", """]""")

-  //      //println("...got: " + result)

-  //      assertEquals(x._2, result)

-  //      //println("test " + idx + " succeeded")

-  //      idx += 1

-  //    })

-  //  }

-  //

-  //  @Test def testEscapeBlockRemoval_Same = {

-  //    // Same Start/End characters

-  //    val qInputOutput = Queue.empty[(String, String)]

-  //    val qOutput = Queue.empty[String]

-  //

-  //    qInputOutput.enqueue("texttext" -> "texttext")

-  //    qInputOutput.enqueue("'%'texttext'" -> "'texttext")

-  //    qInputOutput.enqueue("text'text" -> "text'text")

-  //    qInputOutput.enqueue("texttext'" -> "texttext'")

-  //    qInputOutput.enqueue("'%'%'texttext'" -> "''texttext")

-  //    qInputOutput.enqueue("texttext''" -> "texttext''")

-  //    qInputOutput.enqueue("text''text" -> "text''text")

-  //    qInputOutput.enqueue("'%'texttext%''" -> "'texttext'")

-  //

-  //    qInputOutput.enqueue("'%'text%'text'" -> "'text'text")

-  //    qInputOutput.enqueue("text'text'" -> "text'text'")

-  //    qInputOutput.enqueue("'%'text%'text%''" -> "'text'text'")

-  //    qInputOutput.enqueue("'%'%'texttext%''" -> "''texttext'")

-  //    qInputOutput.enqueue("'%'texttext%'%''" -> "'texttext''")

-  //    qInputOutput.enqueue("'%'%'texttext%'%''" -> "''texttext''")

-  //

-  //    qInputOutput.enqueue("text%text" -> "text%text")

-  //    qInputOutput.enqueue("text%%text" -> "text%%text")

-  //    qInputOutput.enqueue("text%'text" -> "text%'text")

-  //    qInputOutput.enqueue("%'texttext" -> "%'texttext")

-  //

-  //    qInputOutput.enqueue("texttext%'" -> "texttext%'")

-  //    qInputOutput.enqueue("'%'texttext%%''" -> "'texttext%'")

-  //    qInputOutput.enqueue("%'texttext%'" -> "%'texttext%'")

-  //    qInputOutput.enqueue("'%'text%text%''" -> "'text%text'")

-  //    qInputOutput.enqueue("'%'text%%'text%''" -> "'text%'text'")

-  //

-  //    qInputOutput.enqueue("'text;text'" -> "text;text")

-  //    qInputOutput.enqueue("'text%;text'" -> "text%;text")

-  //    qInputOutput.enqueue("'%'text;text%''" -> "'text;text'")

-  //    qInputOutput.enqueue("'text?text'" -> "text?text")

-  //

-  //    val d = new DelimParser(Fakes.fakeElem)

-  //    var idx = 1

-  //    qInputOutput.foreach(x => {

-  //      //println("trying... expect: " + x._2 + " for input: " + x._1)

-  //      val result = d.removeEscapesBlocks(x._1, "%", """'""")

-  //      //println("...got: " + result)

-  //      assertEquals(x._2, result)

-  //      //println("test " + idx + " succeeded")

-  //      idx += 1

-  //    })

-  //  }

-  //

-  //  @Test def testParseSingleFieldFromAB007 = {

-  //    ////println(System.getProperty("user.dir"))

-  //    //val channel = new FileInputStream(testFileDir + "AB007.in").getChannel()

-  //    val channel = new FileInputStream(new File(new URI(rsrcAB007.toString()))).getChannel()

-  //

-  //    val byteR = new DFDLByteReader(channel)

-  //

-  //    //val r = byteR.charReader("UTF-8")

-  //    val r = byteR.newCharReader(Charset.forName("UTF-8"), 0)

-  //

-  //    val d = new DelimParser(Fakes.fakeElem)

-  //

-  //    val separators = Set[String](",")

-  //

-  //    val terminators = Set[String]("%NL;")

-  //

-  //    val res = d.parseInput(separators, terminators, r, TextJustificationType.None, "")

-  //

-  //    assertEquals("1", res.field)

-  //    assertEquals(",", res.delimiter)

-  //  }

-

-  @Test def testParsingEscapeSchemeBlockAtStart = {

-    // Valid escapeBlockStart and escapeBlockEnd

-    // escBS starts at beginning of field AND

-    // escBE ends immediately followed by a delimiter.

-    //

-    val r = new DFDLUTStringReader("/*hidden/*:text*/:def:ghi") // Input 1

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val separators = Set[String](":")

-    val terminators = Set[String]()

-    val escapeBlockStart = "/*"

-    val escapeBlockEnd = "*/"

-    val escapeEscapeCharacter = ""

-

-    val res = d.parseInputEscapeBlock(separators, terminators, r,

-      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")

-

-    res match {

-      case s: DelimParseSuccess => {

-        assertEquals("hidden/*:text", s.field)

-        assertEquals(":", s.delimiter)

-        assertEquals(17 * 8, s.numBits)

-      }

-      case _ => fail()

-    }

-  }

-

-  @Test def testParsingEscapedEscapeSchemeBlockAtStart = {

-    // Not a valid escapeBlockStart as it does not start

-    // at the beginning of the field

-    //

-    val r = new DFDLUTStringReader("//*hidden/*:text*/:def:ghi") // Input 1

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val separators = Set[String](":")

-    val terminators = Set[String]()

-    val escapeBlockStart = "/*"

-    val escapeBlockEnd = "*/"

-    val escapeEscapeCharacter = "/"

-

-    val res = d.parseInputEscapeBlock(separators, terminators, r,

-      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")

-

-    res match {

-      case s: DelimParseSuccess => {

-        assertEquals("//*hidden/*", s.field)

-        assertEquals(":", s.delimiter)

-        assertEquals(11 * 8, s.numBits)

-      }

-      case _ => fail()

-    }

-  }

-

-  @Test def testParsingEscapeSchemeBlockInMiddle = {

-    // Not a valid escapeBlockStart as it does not start

-    // at the beginning of the field

-    //

-    val r = new DFDLUTStringReader("abc/*hidden/*:text*/:def:ghi") // Input 1

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val separators = Set[String](":")

-    val terminators = Set[String]()

-    val escapeBlockStart = "/*"

-    val escapeBlockEnd = "*/"

-    val escapeEscapeCharacter = ""

-

-    val res = d.parseInputEscapeBlock(separators, terminators, r,

-      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")

-

-    res match {

-      case s: DelimParseSuccess => {

-        assertTrue(res.isSuccess)

-        assertEquals("abc/*hidden/*", s.field)

-        assertEquals(":", s.delimiter)

-        assertEquals(13 * 8, s.numBits)

-      }

-      case _ => fail()

-    }

-  }

-

-  @Test def testParsingEscapeSchemeBlock_PartialBlock = {

-    // Because there are no full escape blocks, we expect this to parse

-    // normally

-    //

-    val r = new DFDLUTStringReader("/*abchidden:text:def:ghi") // Input 1

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val separators = Set[String](":")

-    val terminators = Set[String]()

-    val escapeBlockStart = "/*"

-    val escapeBlockEnd = "*/"

-    val escapeEscapeCharacter = ""

-

-    val res = d.parseInputEscapeBlock(separators, terminators, r,

-      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")

-

-    res match {

-      case s: DelimParseSuccess => {

-        assertEquals("/*abchidden", s.field)

-        assertEquals(":", s.delimiter)

-        assertEquals(11 * 8, s.numBits)

-      }

-      case _ => fail()

-    }

-  }

-

-  @Test def testParsingEscapeSchemeBlock_NoBlocks = {

-    // Because there are no escape blocks, we expect this to parse

-    // normally

-    //

-    val r = new DFDLUTStringReader("abchidden*:text:def:ghi") // Input 1

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val separators = Set[String](":")

-    val terminators = Set[String]()

-    val escapeBlockStart = "/*"

-    val escapeBlockEnd = "*/"

-    val escapeEscapeCharacter = ""

-

-    val res = d.parseInputEscapeBlock(separators, terminators, r,

-      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")

-

-    res match {

-      case s: DelimParseSuccess => {

-        assertEquals("abchidden*", s.field)

-        assertEquals(":", s.delimiter)

-        assertEquals(10 * 8, s.numBits)

-      }

-      case _ => fail()

-    }

-  }

-

-  @Test def testParsingEscapeSchemeCharacter_NoEscapes = {

-    // Because there are no escapes present we expect

-    // this to parse normally.

-    //

-    val r = new DFDLUTStringReader("abc:def:ghi") // Input 1

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val separators = Set[String](":")

-    val terminators = Set[String]()

-    val escapeCharacter = "/"

-    val escapeEscapeCharacter = "/"

-

-    val res = d.parseInputEscapeCharacter(separators, terminators, r, escapeCharacter,

-      escapeEscapeCharacter, TextJustificationType.None, "")

-

-    res match {

-      case s: DelimParseSuccess => {

-        assertEquals("abc", s.field)

-        assertEquals(":", s.delimiter)

-        assertEquals(3 * 8, s.numBits)

-      }

-      case _ => fail()

-    }

-  }

-

-  @Test def testParsingEscapeSchemeCharacter_UnescapedEscape = {

-

-    val r = new DFDLUTStringReader("abc/:def:ghi") // Input 1

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val separators = Set[String](":")

-    val terminators = Set[String]()

-    val escapeCharacter = "/"

-    val escapeEscapeCharacter = "/"

-

-    val res = d.parseInputEscapeCharacter(separators, terminators, r, escapeCharacter,

-      escapeEscapeCharacter, TextJustificationType.None, "")

-

-    res match {

-      case s: DelimParseSuccess => {

-        assertEquals("abc:def", s.field)

-        assertEquals(":", s.delimiter)

-        assertEquals(8 * 8, s.numBits)

-      }

-      case _ => fail()

-    }

-  }

-

-  @Test def testParsingEscapeSchemeCharacter_EscapedEscape = {

-

-    val r = new DFDLUTStringReader("abc//:def:ghi") // Input 1

-    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)

-    val separators = Set[String](":")

-    val terminators = Set[String]()

-    val escapeCharacter = "/"

-    val escapeEscapeCharacter = "/"

-

-    val res = d.parseInputEscapeCharacter(separators, terminators, r, escapeCharacter,

-      escapeEscapeCharacter, TextJustificationType.None, "")

-

-    res match {

-      case s: DelimParseSuccess => {

-        assertEquals("abc/", s.field)

-        assertEquals(":", s.delimiter)

-        assertEquals(5 * 8, s.numBits)

-      }

-      case _ => fail()

-    }

-  }

-

+ */
+
+import junit.framework.Assert._
+import scala.collection.mutable.Queue
+import java.util.regex.Pattern
+import java.io.FileInputStream
+import edu.illinois.ncsa.daffodil.util.Misc
+import javax.xml.transform.stream.StreamSource
+import java.io.File
+import java.nio.charset.Charset
+import java.net.URI
+import org.junit.Test
+import edu.illinois.ncsa.daffodil.dsom.Fakes
+import edu.illinois.ncsa.daffodil.processors.DFDLByteReader
+import edu.illinois.ncsa.daffodil.processors.TextJustificationType
+import edu.illinois.ncsa.daffodil.processors.DelimParser
+import edu.illinois.ncsa.daffodil.processors.DelimParseSuccess
+import edu.illinois.ncsa.daffodil.dsom.Fakes
+import edu.illinois.ncsa.daffodil.processors.DFDLUTStringReader
+
+class TestParsingBehaviors {
+
+  val testFileDir = "/test/"
+
+  val rsrcAB007 = Misc.getRequiredResource(testFileDir + "AB007.in")
+
+  @Test def testEscapeCharacterRemoval_Same = {
+    // escape and escapeEscape are the same
+    val input0 = "texttexttext"
+    val input1 = "text1//text2"
+    val input2 = "text1//text2//text3"
+    val input3 = "text1////text2"
+    val input4 = "//text1"
+    val input5 = "text1//"
+    val input6 = "//text1//text2"
+    val input7 = "text1//text2//"
+    val input8 = "text1/,text2"
+    val input9 = "text1///,text2"
+    val input10 = "/,text1"
+
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+
+    assertEquals("texttexttext", d.removeEscapeCharacters(input0, "/", "/", ","))
+    assertEquals("text1/text2", d.removeEscapeCharacters(input1, "/", "/", ","))
+    assertEquals("text1/text2/text3", d.removeEscapeCharacters(input2, "/", "/", ","))
+    assertEquals("text1//text2", d.removeEscapeCharacters(input3, "/", "/", ","))
+    assertEquals("/text1", d.removeEscapeCharacters(input4, "/", "/", ","))
+    assertEquals("text1/", d.removeEscapeCharacters(input5, "/", "/", ","))
+    assertEquals("/text1/text2", d.removeEscapeCharacters(input6, "/", "/", ","))
+    assertEquals("text1/text2/", d.removeEscapeCharacters(input7, "/", "/", ","))
+    assertEquals("text1,text2", d.removeEscapeCharacters(input8, "/", "/", ","))
+    assertEquals("text1/,text2", d.removeEscapeCharacters(input9, "/", "/", ","))
+    assertEquals(",text1", d.removeEscapeCharacters(input10, "/", "/", ","))
+  }
+
+  @Test def testEscapeCharacterRemoval_Diff = {
+    // different escape and escapeEscape characters
+    val input0 = "texttexttext"
+    val input1 = "text1%/text2"
+    val input2 = "text1%/text2%/text3"
+    val input3 = "text1%/%/text2"
+    val input4 = "%/text1"
+    val input5 = "text1%/"
+    val input6 = "%/text1%/text2"
+    val input7 = "text1%/text2%/"
+    val input8 = "text1/,text2"
+    val input9 = "text1%//,text2"
+    val input10 = "/,text1"
+    val input11 = "text1/?text2"
+    val input12 = "text1%text2"
+    val input13 = "text1%%/text2"
+    val input14 = "text1%/%text2"
+
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+
+    assertEquals("texttexttext", d.removeEscapeCharacters(input0, "%", "/", ","))
+    assertEquals("text1/text2", d.removeEscapeCharacters(input1, "%", "/", ","))
+    assertEquals("text1/text2/text3", d.removeEscapeCharacters(input2, "%", "/", ","))
+    assertEquals("text1//text2", d.removeEscapeCharacters(input3, "%", "/", ","))
+    assertEquals("/text1", d.removeEscapeCharacters(input4, "%", "/", ","))
+    assertEquals("text1/", d.removeEscapeCharacters(input5, "%", "/", ","))
+    assertEquals("/text1/text2", d.removeEscapeCharacters(input6, "%", "/", ","))
+    assertEquals("text1/text2/", d.removeEscapeCharacters(input7, "%", "/", ","))
+    assertEquals("text1,text2", d.removeEscapeCharacters(input8, "%", "/", ","))
+    assertEquals("text1/,text2", d.removeEscapeCharacters(input9, "%", "/", ","))
+    assertEquals(",text1", d.removeEscapeCharacters(input10, "%", "/", ","))
+    assertEquals("text1?text2", d.removeEscapeCharacters(input11, "%", "/", ","))
+    assertEquals("text1%text2", d.removeEscapeCharacters(input12, "%", "/", ","))
+    assertEquals("text1%/text2", d.removeEscapeCharacters(input13, "%", "/", ","))
+    assertEquals("text1/%text2", d.removeEscapeCharacters(input14, "%", "/", ","))
+  }
+
+  @Test def testEscapeCharacterRemoval_Diff_MultiCharDelim = {
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val input0 = "text1/septext2"
+    val input1 = "text1%//septext2"
+    val input2 = "/septext1text2"
+
+    assertEquals("text1septext2", d.removeEscapeCharacters(input0, "%", "/", "sep"))
+    assertEquals("text1/septext2", d.removeEscapeCharacters(input1, "%", "/", "sep"))
+    assertEquals("septext1text2", d.removeEscapeCharacters(input2, "%", "/", "sep"))
+  }
+
+  // Invalid, escapeBlockRemoval code expects valid start/end blocks already picked off
+  //  @Test def testEscapeBlockRemoval_Diff = {
+  //    // Different Start/End characters
+  //    val qInputOutput = Queue.empty[(String, String)]
+  //    val qOutput = Queue.empty[String]
+  //
+  //    qInputOutput.enqueue("texttext" -> "texttext")
+  //    qInputOutput.enqueue("[[texttext]" -> "[texttext")
+  //    qInputOutput.enqueue("]texttext" -> "]texttext")
+  //    qInputOutput.enqueue("text[text" -> "text[text")
+  //    qInputOutput.enqueue("text]text" -> "text]text")
+  //    qInputOutput.enqueue("texttext]" -> "texttext]")
+  //    qInputOutput.enqueue("[[[texttext]" -> "[[texttext")
+  //    qInputOutput.enqueue("texttext]]" -> "texttext]]")
+  //    qInputOutput.enqueue("text[[text" -> "text[[text")
+  //    qInputOutput.enqueue("text]]text" -> "text]]text")
+  //    qInputOutput.enqueue("[[texttext%]]" -> "[texttext]")
+  //    qInputOutput.enqueue("[[text%]text]" -> "[text]text")
+  //    qInputOutput.enqueue("text[text]" -> "text[text]")
+  //    qInputOutput.enqueue("[[text[text%]]" -> "[text[text]")
+  //    qInputOutput.enqueue("[[text%]text%]]" -> "[text]text]")
+  //    qInputOutput.enqueue("[[[texttext%]]" -> "[[texttext]")
+  //    qInputOutput.enqueue("[[texttext%]%]]" -> "[texttext]]")
+  //    qInputOutput.enqueue("[[[texttext%]%]]" -> "[[texttext]]")
+  //    qInputOutput.enqueue("text%text" -> "text%text")
+  //    qInputOutput.enqueue("text%%text" -> "text%%text")
+  //    qInputOutput.enqueue("text%[text" -> "text%[text")
+  //    qInputOutput.enqueue("text%]text" -> "text%]text")
+  //    qInputOutput.enqueue("%[texttext" -> "%[texttext")
+  //    qInputOutput.enqueue("texttext%]" -> "texttext%]")
+  //    qInputOutput.enqueue("%[texttext%]" -> "%[texttext%]")
+  //    qInputOutput.enqueue("[[text%text%]]" -> "[text%text]")
+  //    qInputOutput.enqueue("[[text%%]text%]]" -> "[text%]text]")
+  //    qInputOutput.enqueue("[text;text]" -> "text;text")
+  //    qInputOutput.enqueue("[text%;text]" -> "text%;text")
+  //    qInputOutput.enqueue("[[text;text%]]" -> "[text;text]")
+  //    qInputOutput.enqueue("[text?text]" -> "text?text")
+  //
+  //    val d = new DelimParser(Fakes.fakeElem)
+  //    var idx = 1
+  //    qInputOutput.foreach(x => {
+  //      println("trying... expect: " + x._2 + " for input: " + x._1)
+  //      val result = d.removeEscapesBlocks(x._1, "%", """]""")
+  //      //println("...got: " + result)
+  //      assertEquals(x._2, result)
+  //      //println("test " + idx + " succeeded")
+  //      idx += 1
+  //    })
+  //  }
+  //
+  //  @Test def testEscapeBlockRemoval_Same = {
+  //    // Same Start/End characters
+  //    val qInputOutput = Queue.empty[(String, String)]
+  //    val qOutput = Queue.empty[String]
+  //
+  //    qInputOutput.enqueue("texttext" -> "texttext")
+  //    qInputOutput.enqueue("'%'texttext'" -> "'texttext")
+  //    qInputOutput.enqueue("text'text" -> "text'text")
+  //    qInputOutput.enqueue("texttext'" -> "texttext'")
+  //    qInputOutput.enqueue("'%'%'texttext'" -> "''texttext")
+  //    qInputOutput.enqueue("texttext''" -> "texttext''")
+  //    qInputOutput.enqueue("text''text" -> "text''text")
+  //    qInputOutput.enqueue("'%'texttext%''" -> "'texttext'")
+  //
+  //    qInputOutput.enqueue("'%'text%'text'" -> "'text'text")
+  //    qInputOutput.enqueue("text'text'" -> "text'text'")
+  //    qInputOutput.enqueue("'%'text%'text%''" -> "'text'text'")
+  //    qInputOutput.enqueue("'%'%'texttext%''" -> "''texttext'")
+  //    qInputOutput.enqueue("'%'texttext%'%''" -> "'texttext''")
+  //    qInputOutput.enqueue("'%'%'texttext%'%''" -> "''texttext''")
+  //
+  //    qInputOutput.enqueue("text%text" -> "text%text")
+  //    qInputOutput.enqueue("text%%text" -> "text%%text")
+  //    qInputOutput.enqueue("text%'text" -> "text%'text")
+  //    qInputOutput.enqueue("%'texttext" -> "%'texttext")
+  //
+  //    qInputOutput.enqueue("texttext%'" -> "texttext%'")
+  //    qInputOutput.enqueue("'%'texttext%%''" -> "'texttext%'")
+  //    qInputOutput.enqueue("%'texttext%'" -> "%'texttext%'")
+  //    qInputOutput.enqueue("'%'text%text%''" -> "'text%text'")
+  //    qInputOutput.enqueue("'%'text%%'text%''" -> "'text%'text'")
+  //
+  //    qInputOutput.enqueue("'text;text'" -> "text;text")
+  //    qInputOutput.enqueue("'text%;text'" -> "text%;text")
+  //    qInputOutput.enqueue("'%'text;text%''" -> "'text;text'")
+  //    qInputOutput.enqueue("'text?text'" -> "text?text")
+  //
+  //    val d = new DelimParser(Fakes.fakeElem)
+  //    var idx = 1
+  //    qInputOutput.foreach(x => {
+  //      //println("trying... expect: " + x._2 + " for input: " + x._1)
+  //      val result = d.removeEscapesBlocks(x._1, "%", """'""")
+  //      //println("...got: " + result)
+  //      assertEquals(x._2, result)
+  //      //println("test " + idx + " succeeded")
+  //      idx += 1
+  //    })
+  //  }
+  //
+  //  @Test def testParseSingleFieldFromAB007 = {
+  //    ////println(System.getProperty("user.dir"))
+  //    //val channel = new FileInputStream(testFileDir + "AB007.in").getChannel()
+  //    val channel = new FileInputStream(new File(new URI(rsrcAB007.toString()))).getChannel()
+  //
+  //    val byteR = new DFDLByteReader(channel)
+  //
+  //    //val r = byteR.charReader("UTF-8")
+  //    val r = byteR.newCharReader(Charset.forName("UTF-8"), 0)
+  //
+  //    val d = new DelimParser(Fakes.fakeElem)
+  //
+  //    val separators = Set[String](",")
+  //
+  //    val terminators = Set[String]("%NL;")
+  //
+  //    val res = d.parseInput(separators, terminators, r, TextJustificationType.None, "")
+  //
+  //    assertEquals("1", res.field)
+  //    assertEquals(",", res.delimiter)
+  //  }
+
+  @Test def testParsingEscapeSchemeBlockAtStart = {
+    // Valid escapeBlockStart and escapeBlockEnd
+    // escBS starts at beginning of field AND
+    // escBE ends immediately followed by a delimiter.
+    //
+    val r = new DFDLUTStringReader("/*hidden/*:text*/:def:ghi") // Input 1
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val separators = Set[String](":")
+    val terminators = Set[String]()
+    val escapeBlockStart = "/*"
+    val escapeBlockEnd = "*/"
+    val escapeEscapeCharacter = ""
+
+    val res = d.parseInputEscapeBlock(separators, terminators, r,
+      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")
+
+    res match {
+      case s: DelimParseSuccess => {
+        assertEquals("hidden/*:text", s.field)
+        assertEquals(":", s.delimiter)
+        assertEquals(17 * 8, s.numBits)
+      }
+      case _ => fail()
+    }
+  }
+
+  @Test def testParsingEscapedEscapeSchemeBlockAtStart = {
+    // Not a valid escapeBlockStart as it does not start
+    // at the beginning of the field
+    //
+    val r = new DFDLUTStringReader("//*hidden/*:text*/:def:ghi") // Input 1
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val separators = Set[String](":")
+    val terminators = Set[String]()
+    val escapeBlockStart = "/*"
+    val escapeBlockEnd = "*/"
+    val escapeEscapeCharacter = "/"
+
+    val res = d.parseInputEscapeBlock(separators, terminators, r,
+      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")
+
+    res match {
+      case s: DelimParseSuccess => {
+        assertEquals("//*hidden/*", s.field)
+        assertEquals(":", s.delimiter)
+        assertEquals(11 * 8, s.numBits)
+      }
+      case _ => fail()
+    }
+  }
+
+  @Test def testParsingEscapeSchemeBlockInMiddle = {
+    // Not a valid escapeBlockStart as it does not start
+    // at the beginning of the field
+    //
+    val r = new DFDLUTStringReader("abc/*hidden/*:text*/:def:ghi") // Input 1
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val separators = Set[String](":")
+    val terminators = Set[String]()
+    val escapeBlockStart = "/*"
+    val escapeBlockEnd = "*/"
+    val escapeEscapeCharacter = ""
+
+    val res = d.parseInputEscapeBlock(separators, terminators, r,
+      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")
+
+    res match {
+      case s: DelimParseSuccess => {
+        assertTrue(res.isSuccess)
+        assertEquals("abc/*hidden/*", s.field)
+        assertEquals(":", s.delimiter)
+        assertEquals(13 * 8, s.numBits)
+      }
+      case _ => fail()
+    }
+  }
+
+  @Test def testParsingEscapeSchemeBlock_PartialBlock = {
+    // Because there are no full escape blocks, we expect this to parse
+    // normally
+    //
+    val r = new DFDLUTStringReader("/*abchidden:text:def:ghi") // Input 1
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val separators = Set[String](":")
+    val terminators = Set[String]()
+    val escapeBlockStart = "/*"
+    val escapeBlockEnd = "*/"
+    val escapeEscapeCharacter = ""
+
+    val res = d.parseInputEscapeBlock(separators, terminators, r,
+      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")
+
+    res match {
+      case s: DelimParseSuccess => {
+        assertEquals("/*abchidden", s.field)
+        assertEquals(":", s.delimiter)
+        assertEquals(11 * 8, s.numBits)
+      }
+      case _ => fail()
+    }
+  }
+
+  @Test def testParsingEscapeSchemeBlock_NoBlocks = {
+    // Because there are no escape blocks, we expect this to parse
+    // normally
+    //
+    val r = new DFDLUTStringReader("abchidden*:text:def:ghi") // Input 1
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val separators = Set[String](":")
+    val terminators = Set[String]()
+    val escapeBlockStart = "/*"
+    val escapeBlockEnd = "*/"
+    val escapeEscapeCharacter = ""
+
+    val res = d.parseInputEscapeBlock(separators, terminators, r,
+      escapeBlockStart, escapeBlockEnd, escapeEscapeCharacter, TextJustificationType.None, "")
+
+    res match {
+      case s: DelimParseSuccess => {
+        assertEquals("abchidden*", s.field)
+        assertEquals(":", s.delimiter)
+        assertEquals(10 * 8, s.numBits)
+      }
+      case _ => fail()
+    }
+  }
+
+  @Test def testParsingEscapeSchemeCharacter_NoEscapes = {
+    // Because there are no escapes present we expect
+    // this to parse normally.
+    //
+    val r = new DFDLUTStringReader("abc:def:ghi") // Input 1
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val separators = Set[String](":")
+    val terminators = Set[String]()
+    val escapeCharacter = "/"
+    val escapeEscapeCharacter = "/"
+
+    val res = d.parseInputEscapeCharacter(separators, terminators, r, escapeCharacter,
+      escapeEscapeCharacter, TextJustificationType.None, "")
+
+    res match {
+      case s: DelimParseSuccess => {
+        assertEquals("abc", s.field)
+        assertEquals(":", s.delimiter)
+        assertEquals(3 * 8, s.numBits)
+      }
+      case _ => fail()
+    }
+  }
+
+  @Test def testParsingEscapeSchemeCharacter_UnescapedEscape = {
+
+    val r = new DFDLUTStringReader("abc/:def:ghi") // Input 1
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val separators = Set[String](":")
+    val terminators = Set[String]()
+    val escapeCharacter = "/"
+    val escapeEscapeCharacter = "/"
+
+    val res = d.parseInputEscapeCharacter(separators, terminators, r, escapeCharacter,
+      escapeEscapeCharacter, TextJustificationType.None, "")
+
+    res match {
+      case s: DelimParseSuccess => {
+        assertEquals("abc:def", s.field)
+        assertEquals(":", s.delimiter)
+        assertEquals(8 * 8, s.numBits)
+      }
+      case _ => fail()
+    }
+  }
+
+  @Test def testParsingEscapeSchemeCharacter_EscapedEscape = {
+
+    val r = new DFDLUTStringReader("abc//:def:ghi") // Input 1
+    val d = new DelimParser(Fakes.fakeElem.knownEncodingStringBitLengthFunction)
+    val separators = Set[String](":")
+    val terminators = Set[String]()
+    val escapeCharacter = "/"
+    val escapeEscapeCharacter = "/"
+
+    val res = d.parseInputEscapeCharacter(separators, terminators, r, escapeCharacter,
+      escapeEscapeCharacter, TextJustificationType.None, "")
+
+    res match {
+      case s: DelimParseSuccess => {
+        assertEquals("abc/", s.field)
+        assertEquals(":", s.delimiter)
+        assertEquals(5 * 8, s.numBits)
+      }
+      case _ => fail()
+    }
+  }
+
 }
\ No newline at end of file
diff --git a/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/processors/input/TestDFDLRegularExpressions.scala b/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/processors/input/TestDFDLRegularExpressions.scala
index 7ec5c89..2f218b3 100644
--- a/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/processors/input/TestDFDLRegularExpressions.scala
+++ b/daffodil-core-unittest/src/test/scala/edu/illinois/ncsa/daffodil/processors/input/TestDFDLRegularExpressions.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.processors.input

+package edu.illinois.ncsa.daffodil.processors.input
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,705 +32,705 @@
  * SOFTWARE.
  */
 
-

-import junit.framework.Assert._

-import scala.util.parsing.combinator._

-import java.io.StringReader

-import org.junit.Test

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-

-import edu.illinois.ncsa.daffodil.processors.DFDLRegularExpressions

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification

-

-/**

- * The purpose of this class is to verify that the configurable

- * nature of the regular expressions does not negatively affect

- * the results.  Also verifies that the expressions still work

- * when multiple delimiters are used.

- */

-class TestDFDLRegularExpressions {

-

-  def escape = "E"

-  def escapeEscape = "S"

-  def delim = """\_*D\_*"""

-  def padChar = "P"

-  def bStart = "T"

-  def bEnd = "N"

-

-  def delims = "D|C|F"

-

-  @Test def testSameEscapeRegExNoPadding = {

-    val cp = DFDLRegularExpressions.getSameEscapeRegEx(escape, delim)

-    def test(x: String) = x match {

-      case cp(before, rubbish, delim, after) => {

-        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)

-        Some((before, delim))

-      }

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test("beforeDafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test("beforeEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test("beforeEEDstillBeforeDafter"))

-    assertEquals(None, test("beforeE"))

-    assertEquals(Some("PPPbeforeEDstillBeforePPP", "D"), test("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforeDafter"))

-    assertEquals(Some("beforeEEEDstillBefore", "D"), test("beforeEEEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test("beforeEEEEDafter"))

-

-    // What about multiple delimiters?

-    val cp2 = DFDLRegularExpressions.getSameEscapeRegEx(escape, delims)

-    def test2(x: String) = x match {

-      case cp2(before, rubbish, delim, after) => {

-        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)

-        Some((before, delim))

-      }

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test2("beforeDafter"))

-    assertEquals(Some("before", "C"), test2("beforeCafter"))

-    assertEquals(Some("before", "F"), test2("beforeFafter"))

-    assertEquals(None, test2("beforeGafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test2("beforeEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test2("beforeEEDstillBeforeDafter"))

-    assertEquals(None, test2("beforeE"))

-    assertEquals(Some("PPPbeforeEDstillBeforePPP", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforeDafter"))

-    assertEquals(Some("beforeEEEDstillBefore", "D"), test2("beforeEEEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test2("beforeEEEEDafter"))

-  }

-

-  @Test def testSameEscapeRegExLeftJustified = {

-    val cp = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delim, padChar, TextStringJustification.Left)

-    def test(x: String) = x match {

-      case cp(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {

-        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)

-        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad

-        Some((before, delim1))

-      }

-      case cp(before, rubbish, delim, null, null, after) => {

-        Some((before, delim))

-      }

-      case cp(before, null, null, ee, delim, after) => {

-        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)

-        Some((before, delim))

-      }

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test("beforeDafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test("beforeEDstillBeforePPPDafter"))

-    assertEquals(Some("before", "D"), test("beforeEEDstillBeforeDafter"))

-    assertEquals(None, test("beforeE"))

-    assertEquals(Some("PPPbeforeEDstillBefore", "D"), test("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforeDafter"))

-    assertEquals(Some("beforeEEEDstillBefore", "D"), test("beforeEEEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test("beforeEEEEDafter"))

-

-    // What about multiple delimiters?

-    val cp2 = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delims, padChar, TextStringJustification.Left)

-    def test2(x: String) = x match {

-      case cp2(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {

-        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)

-        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad

-        Some((before, delim1))

-      }

-      case cp2(before, rubbish, delim, null, null, after) => {

-        Some((before, delim))

-      }

-      case cp2(before, null, null, ee, delim, after) => {

-        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)

-        Some((before, delim))

-      }

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test2("beforeDafter"))

-    assertEquals(Some("before", "C"), test2("beforeCafter"))

-    assertEquals(Some("before", "F"), test2("beforeFafter"))

-    assertEquals(None, test2("beforeGafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test2("beforeEDstillBeforePPPDafter"))

-    assertEquals(Some("before", "D"), test2("beforeEEDstillBeforeDafter"))

-    assertEquals(None, test("beforeE"))

-    assertEquals(Some("PPPbeforeEDstillBefore", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforeDafter"))

-    assertEquals(Some("beforeEEEDstillBefore", "D"), test2("beforeEEEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test2("beforeEEEEDafter"))

-  }

-

-  @Test def testSameEscapeRegExRightJustified = {

-    val cp = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delim, padChar, TextStringJustification.Right)

-    def test(x: String) = x match {

-      case cp(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {

-        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)

-        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad

-        Some((before, delim1))

-      }

-      case cp(before, rubbish, delim, null, null, after) => {

-        Some((before, delim))

-      }

-      case cp(before, null, null, ee, delim, after) => {

-        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)

-        Some((before, delim))

-      }

-      case cp(rubbish, before, rubbish2, delim, after) => {

-        Some((before, delim))

-      }

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test("beforeDafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test("PPPbeforeEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test("PPPbeforeEEDstillBeforeDafter"))

-    assertEquals(None, test("beforeE"))

-    assertEquals(Some("beforeEDstillBeforePPP", "D"), test("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforePPPDafter"))

-    assertEquals(Some("beforeEEEDstillBefore", "D"), test("beforeEEEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test("beforeEEEEDafter"))

-

-    // What about multiple delimiters?

-    val cp2 = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delims, padChar, TextStringJustification.Right)

-    def test2(x: String) = x match {

-      case cp2(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {

-        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)

-        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad

-        Some((before, delim1))

-      }

-      case cp2(before, rubbish, delim, null, null, after) => {

-        Some((before, delim))

-      }

-      case cp2(before, null, null, ee, delim, after) => {

-        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)

-        Some((before, delim))

-      }

-      case cp2(rubbish, before, rubbish2, delim, after) => {

-        Some((before, delim))

-      }

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test2("beforeDafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test2("PPPbeforeEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test2("PPPbeforeEEDstillBeforeDafter"))

-    assertEquals(None, test2("beforeE"))

-    assertEquals(Some("beforeEDstillBeforePPP", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforePPPDafter"))

-    assertEquals(Some("beforeEEEDstillBefore", "D"), test2("beforeEEEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test2("beforeEEEEDafter"))

-  }

-

-  @Test def testSameEscapeRegExCenterJustified = {

-    val cp = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delim, padChar, TextStringJustification.Center)

-    def test(x: String) = x match {

-      case cp(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {

-        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)

-        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad

-        Some((before, delim1))

-      }

-      case cp(before, rubbish, delim, null, null, after) => {

-        Some((before, delim))

-      }

-      case cp(before, null, null, ee, delim, after) => {

-        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)

-        Some((before, delim))

-      }

-      case cp(rubbish, before, rubbish2, delim, after) => {

-        Some((before, delim))

-      }

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test("beforeDafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforePPP", "D"), test("PPPbeforePPPEEDafter"))

-    assertEquals(None, test("beforeE"))

-    assertEquals(Some("beforePPPEDstillBefore", "D"), test("PPPbeforePPPEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforePPPDafter"))

-    assertEquals(Some("beforeEEEDstillBefore", "D"), test("beforeEEEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test("beforeEEEEDafter"))

-

-    // What about multiple delimiters?

-    val cp2 = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delims, padChar, TextStringJustification.Center)

-    def test2(x: String) = x match {

-      case cp2(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {

-        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)

-        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad

-        Some((before, delim1))

-      }

-      case cp2(before, rubbish, delim, null, null, after) => {

-        Some((before, delim))

-      }

-      case cp2(before, null, null, ee, delim, after) => {

-        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)

-        Some((before, delim))

-      }

-      case cp2(rubbish, before, rubbish2, delim, after) => {

-        Some((before, delim))

-      }

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test2("beforeDafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforePPP", "D"), test2("PPPbeforePPPEEDafter"))

-    assertEquals(None, test("beforeE"))

-    assertEquals(Some("beforePPPEDstillBefore", "D"), test2("PPPbeforePPPEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforePPPDafter"))

-    assertEquals(Some("beforeEEEDstillBefore", "D"), test2("beforeEEEDstillBeforeDafter"))

-    assertEquals(Some("before", "D"), test2("beforeEEEEDafter"))

-  }

-

-  @Test def testEscapeRegExNoPadding = {

-    val cp = DFDLRegularExpressions.getEscapeRegEx(escape, escapeEscape, delim)

-

-    def test(x: String) = x match {

-      case cp(before, delim, blah, after) => Some((before, delim))

-      case cp(before, delim, after) => Some((before, delim))

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test("beforeDafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test("beforeEDstillBeforeDafter"))

-    assertEquals(Some("beforeSE", "D"), test("beforeSEDstillBeforeDafter"))

-    assertEquals(Some("beforeEEDstillBefore", "D"), test("beforeEEDstillBeforeDafter"))

-    assertEquals(None, test("beforeE"))

-    assertEquals(Some("PPPbeforeEDstillBeforePPP", "D"), test("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforeDafter"))

-

-    // Multiple delimiters

-    val cp2 = DFDLRegularExpressions.getEscapeRegEx(escape, escapeEscape, delims)

-

-    def test2(x: String) = x match {

-      case cp2(before, delim, blah, after) => Some((before, delim))

-      case cp2(before, delim, after) => Some((before, delim))

-      case z => None

-    }

-    assertEquals(Some("before", "D"), test2("beforeDafter"))

-    assertEquals(Some("beforeEDstillBefore", "D"), test2("beforeEDstillBeforeDafter"))

-    assertEquals(Some("beforeSE", "D"), test2("beforeSEDstillBeforeDafter"))

-    assertEquals(Some("beforeEEDstillBefore", "D"), test2("beforeEEDstillBeforeDafter"))

-    assertEquals(None, test2("beforeE"))

-    assertEquals(Some("PPPbeforeEDstillBeforePPP", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))

-    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforeDafter"))

-  }

-

-  @Test def testEscapeRegExLeftJustified = {

-    val cp = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delim, padChar, TextStringJustification.Left)

-    def test(x: String) = x match {

-      case cp(before, delim, after) => Some((before, delim))

-      case z => None

-    }

-    assertEquals(Some(("PPPbefore", "D")), test("PPPbeforePPPDafter"))

-    assertEquals(Some(("PPPbefore", "D")), test("PPPbeforeDafter"))

-    assertEquals(Some(("before", "D")), test("beforeDafter"))

-    assertEquals(None, test("PPPbeforeEDafter"))

-

-    // Multiple Delimiters

-    val cp2 = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delims, padChar, TextStringJustification.Left)

-    def test2(x: String) = x match {

-      case cp2(before, delim, after) => Some((before, delim))

-      case z => None

-    }

-    assertEquals(Some(("PPPbefore", "D")), test2("PPPbeforePPPDafter"))

-    assertEquals(Some(("PPPbefore", "D")), test2("PPPbeforeDafter"))

-    assertEquals(Some(("before", "D")), test2("beforeDafter"))

-    assertEquals(None, test2("PPPbeforeEDafter"))

-    assertEquals(Some(("before", "C")), test2("beforeCafter"))

-    assertEquals(Some(("before", "F")), test2("beforeFafter"))

-  }

-

-  @Test def testEscapeRegExRightJustified = {

-    val cp = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delim, padChar, TextStringJustification.Right)

-    def test(x: String) = x match {

-      case cp(before, delim, after) => Some((before, delim))

-      case z => None

-    }

-    assertEquals(Some(("beforePPP", "D")), test("PPPbeforePPPDafter"))

-    assertEquals(Some(("beforePPP", "D")), test("beforePPPDafter"))

-    assertEquals(Some(("before", "D")), test("beforeDafter"))

-    assertEquals(None, test("PPPbeforeEDafter"))

-

-    // Multiple Delimiters

-    val cp2 = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delims, padChar, TextStringJustification.Right)

-    def test2(x: String) = x match {

-      case cp2(before, delim, after) => Some((before, delim))

-      case z => None

-    }

-    assertEquals(Some(("beforePPP", "D")), test2("PPPbeforePPPDafter"))

-    assertEquals(Some(("beforePPP", "D")), test2("beforePPPDafter"))

-    assertEquals(Some(("before", "D")), test2("beforeDafter"))

-    assertEquals(None, test("PPPbeforeEDafter"))

-    assertEquals(Some(("before", "C")), test2("beforeCafter"))

-    assertEquals(Some(("before", "F")), test2("beforeFafter"))

-  }

-

-  @Test def testEscapeRegExCenterJustified = {

-    val cp = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delim, padChar, TextStringJustification.Center)

-    def test(x: String) = x match {

-      case cp(before, delim, after) => Some((before, delim))

-      case z => None

-    }

-    assertEquals(Some(("beforePPPEDmore", "D")), test("PPPbeforePPPEDmoreDafter"))

-    //assertEquals(Some(("beforePPPEDmoreEP", "D")), test("PPPbeforePPPEDmoreEPPPDafter"))

-    assertEquals(Some(("before", "D")), test("PPPbeforePPPDafter"))

-    assertEquals(Some(("before", "D")), test("beforeDafter"))

-    assertEquals(Some(("before", "D")), test("beforePPPDafter"))

-    assertEquals(Some(("before", "D")), test("PPPbeforeDafter"))

-    assertEquals(None, test("PPPbeforeEDafter"))

-

-    // Multiple Delimiters

-    val cp2 = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delims, padChar, TextStringJustification.Center)

-    def test2(x: String) = x match {

-      case cp2(before, delim, after) => Some((before, delim))

-      case z => None

-    }

-    assertEquals(Some(("beforePPPEDmore", "D")), test2("PPPbeforePPPEDmoreDafter"))

-    //assertEquals(Some(("beforePPPEDmoreEP", "D")), test("PPPbeforePPPEDmoreEPPPDafter"))

-    assertEquals(Some(("before", "D")), test2("PPPbeforePPPDafter"))

-    assertEquals(Some(("before", "D")), test2("beforeDafter"))

-    assertEquals(Some(("before", "D")), test2("beforePPPDafter"))

-    assertEquals(Some(("before", "D")), test2("PPPbeforeDafter"))

-    assertEquals(None, test2("PPPbeforeEDafter"))

-    assertEquals(Some(("before", "C")), test2("beforeCafter"))

-    assertEquals(Some(("before", "F")), test2("beforeFafter"))

-  }

-

-  @Test def testEscapeBlockRegExNoPadding = {

-    val cp = DFDLRegularExpressions.getEscapeBlockRegEx(bStart, bEnd, escapeEscape, escape, delim)

-    def test(x: String) = x match {

-      case cp(before, delim, after, null, null, null) => {

-        Some((before, delim, after))

-      }

-      case cp(null, null, null, before, delim, after) => {

-        Some((before, delim, after))

-      }

-      case z => {

-        // println("no match: " + z); 

-        None

-      }

-    }

-    // This particular regex lets you escape either the blockstart or blockend, or the delimiter

-

-    // no blockstart/end

-    assertEquals(Some(("before", "D", "after")), test("beforeDafter"))

-

-    // no blockstart/end, but escape the delimiter

-    assertEquals(Some(("beforeEDstillBefore", "D", "after")), test("beforeEDstillBeforeDafter"))

-

-    // with blockstart/end

-    assertEquals(Some(("beforeDstillBefore", "D", "after")), test("TbeforeDstillBeforeNDafter"))

-

-    // with blockstart/end and esc and escEsc found inside (where they are inactive)

-    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test("TbeforeEDstillBeforeSEDstillBeforeNDafter"))

-    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.

-

-    // with blockstart/end, escape the first block end

-    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test("TbeforeDstillBeforeENstillBeforeNDafter"))

-

-    // with blockstart/end, escapeEscape the escape of the first block end

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test("TbeforeDstillBeforeTstillBeforeSENDafter"))

-

-    // with blockstart, but escape it so it's not really a block.

-    assertEquals(Some(("ETbefore", "D", "afterNstillafter")), test("ETbeforeDafterNstillafter"))

-

-    // Multiple delimiters

-    val cp2 = DFDLRegularExpressions.getEscapeBlockRegEx(bStart, bEnd, escapeEscape, escape, delims)

-    def test2(x: String) = x match {

-      case cp2(before, delim, after, null, null, null) => {

-        Some((before, delim, after))

-      }

-      case cp2(null, null, null, before, delim, after) => {

-        Some((before, delim, after))

-      }

-      case z => {

-        // println("no match: " + z); 

-        None

-      }

-    }

-    // This particular regex lets you escape either the blockstart or blockend, or the delimiter

-

-    // no blockstart/end

-    assertEquals(Some(("before", "D", "after")), test2("beforeDafter"))

-

-    // no blockstart/end, but escape the delimiter

-    assertEquals(Some(("beforeEDstillBefore", "D", "after")), test2("beforeEDstillBeforeDafter"))

-

-    // with blockstart/end

-    assertEquals(Some(("beforeDstillBefore", "D", "after")), test2("TbeforeDstillBeforeNDafter"))

-

-    // with blockstart/end and esc and escEsc found inside (where they are inactive)

-    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test2("TbeforeEDstillBeforeSEDstillBeforeNDafter"))

-    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.

-

-    // with blockstart/end, escape the first block end

-    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test2("TbeforeDstillBeforeENstillBeforeNDafter"))

-

-    // with blockstart/end, escapeEscape the escape of the first block end

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test2("TbeforeDstillBeforeTstillBeforeSENDafter"))

-

-    // with blockstart, but escape it so it's not really a block.

-    assertEquals(Some(("ETbefore", "D", "afterNstillafter")), test2("ETbeforeDafterNstillafter"))

-

-    assertEquals(Some(("beforeDstillBeforeENstillBefore", "C", "after")), test2("TbeforeDstillBeforeENstillBeforeNCafter"))

-    assertEquals(Some(("beforeDstillBeforeENstillBefore", "F", "after")), test2("TbeforeDstillBeforeENstillBeforeNFafter"))

-  }

-

-  @Test def testEscapeBlockRegExLeftJustified = {

-    val cp = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delim, TextStringJustification.Left)

-    def test(x: String) = x match {

-      case cp(before, delim, after, null, null, null) => {

-        Some((before, delim, after))

-      }

-      case cp(null, null, null, before, delim, after) => {

-        Some((before, delim, after))

-      }

-      case z => {

-        // println("no match: " + z); 

-        None

-      }

-    }

-    // This particular regex lets you escape either the blockstart or blockend, or the delimiter

-

-    // Because this is Left justified the padding at the start of the field is actually

-    // considered to be content.  This breaks the normal behavior of the blockstart and blockend

-    // because it is required that blockstart must occur at the very beginning of the content. 

-

-    // no blockstart/end

-    assertEquals(Some(("PPPbefore", "D", "after")), test("PPPbeforePPPDafter"))

-

-    // no blockstart/end, but escape the delimiter

-    assertEquals(Some(("PPPbeforeEDstillBefore", "D", "after")), test("PPPbeforeEDstillBeforePPPDafter"))

-

-    // with blockstart/end

-    assertEquals(Some(("PPPTbefore", "D", "stillBeforeNPPPDafter")), test("PPPTbeforeDstillBeforeNPPPDafter"))

-

-    // with blockstart/end and esc and escEsc found inside (where they are inactive)

-    assertEquals(Some(("PPPTbeforeEDstillBeforeSE", "D", "stillBeforeNPPPDafter")), test("PPPTbeforeEDstillBeforeSEDstillBeforeNPPPDafter"))

-    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.

-

-    // with blockstart/end, escape the first block end

-    assertEquals(Some(("PPPTbefore", "D", "stillBeforeENstillBeforeNPPPDafter")), test("PPPTbeforeDstillBeforeENstillBeforeNPPPDafter"))

-

-    // with blockstart/end, escapeEscape the escape of the first block end

-    assertEquals(Some(("PPPTbefore", "D", "stillBeforeTstillBeforeSENPPPDafter")), test("PPPTbeforeDstillBeforeTstillBeforeSENPPPDafter"))

-

-    // with blockstart, but escape it so it's not really a block.

-    assertEquals(Some(("PPPETbefore", "D", "afterNstillafter")), test("PPPETbeforePPPDafterNstillafter"))

-

-    // here it would appear that the additional padding to the left of the block start

-    // invalidates the escape scheme as we were only expecting padding to the right of block end

-    assertEquals(Some(("PPPTbefore", "D", "NPPPDafter")), test("PPPTbeforeDNPPPDafter"))

-

-    // Multiple Delimiters

-    val cp2 = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delims, TextStringJustification.Left)

-    def test2(x: String) = x match {

-      case cp2(before, delim, after, null, null, null) => {

-        Some((before, delim, after))

-      }

-      case cp2(null, null, null, before, delim, after) => {

-        Some((before, delim, after))

-      }

-      case z => {

-        // println("no match: " + z); 

-        None

-      }

-    }

-    // This particular regex lets you escape either the blockstart or blockend, or the delimiter

-

-    // Because this is Left justified the padding at the start of the field is actually

-    // considered to be content.  This breaks the normal behavior of the blockstart and blockend

-    // because it is required that blockstart must occur at the very beginning of the content. 

-

-    // no blockstart/end

-    assertEquals(Some(("PPPbefore", "D", "after")), test2("PPPbeforePPPDafter"))

-

-    // no blockstart/end, but escape the delimiter

-    assertEquals(Some(("PPPbeforeEDstillBefore", "D", "after")), test2("PPPbeforeEDstillBeforePPPDafter"))

-

-    // with blockstart/end

-    assertEquals(Some(("PPPTbefore", "D", "stillBeforeNPPPDafter")), test2("PPPTbeforeDstillBeforeNPPPDafter"))

-

-    // with blockstart/end and esc and escEsc found inside (where they are inactive)

-    assertEquals(Some(("PPPTbeforeEDstillBeforeSE", "D", "stillBeforeNPPPDafter")), test2("PPPTbeforeEDstillBeforeSEDstillBeforeNPPPDafter"))

-    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.

-

-    // with blockstart/end, escape the first block end

-    assertEquals(Some(("PPPTbefore", "D", "stillBeforeENstillBeforeNPPPDafter")), test2("PPPTbeforeDstillBeforeENstillBeforeNPPPDafter"))

-

-    // with blockstart/end, escapeEscape the escape of the first block end

-    assertEquals(Some(("PPPTbefore", "D", "stillBeforeTstillBeforeSENPPPDafter")), test2("PPPTbeforeDstillBeforeTstillBeforeSENPPPDafter"))

-

-    // with blockstart, but escape it so it's not really a block.

-    assertEquals(Some(("PPPETbefore", "D", "afterNstillafter")), test2("PPPETbeforePPPDafterNstillafter"))

-

-    // here it would appear that the additional padding to the left of the block start

-    // invalidates the escape scheme as we were only expecting padding to the right of block end

-    assertEquals(Some(("PPPTbefore", "D", "NPPPDafter")), test2("PPPTbeforeDNPPPDafter"))

-

-    assertEquals(Some(("PPPTbefore", "C", "stillBeforeTstillBeforeSENPPPDafter")), test2("PPPTbeforeCstillBeforeTstillBeforeSENPPPDafter"))

-    assertEquals(Some(("PPPTbefore", "F", "stillBeforeTstillBeforeSENPPPDafter")), test2("PPPTbeforeFstillBeforeTstillBeforeSENPPPDafter"))

-  }

-

-  @Test def testEscapeBlockRegExRightJustified = {

-    val cp = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delim, TextStringJustification.Right)

-    def test(x: String) = x match {

-      case cp(before, delim, after, null, null, null) => {

-        Some((before, delim, after))

-      }

-      case cp(null, null, null, before, delim, after) => {

-        Some((before, delim, after))

-      }

-      case z => {

-        // println("no match: " + z); 

-        None

-      }

-    }

-    // This particular regex lets you escape either the blockstart or blockend, or the delimiter

-

-    // no blockstart/end

-    assertEquals(Some(("beforePPP", "D", "after")), test("PPPbeforePPPDafter"))

-

-    // no blockstart/end, but escape the delimiter

-    assertEquals(Some(("beforeEDstillBeforePPP", "D", "after")), test("PPPbeforeEDstillBeforePPPDafter"))

-

-    // with blockstart/end

-    assertEquals(Some(("beforeDstillBefore", "D", "after")), test("PPPTbeforeDstillBeforeNDafter"))

-

-    // with blockstart/end and esc and escEsc found inside (where they are inactive)

-    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test("PPPTbeforeEDstillBeforeSEDstillBeforeNDafter"))

-    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.

-

-    // with blockstart/end, escape the first block end

-    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test("PPPTbeforeDstillBeforeENstillBeforeNDafter"))

-

-    // with blockstart/end, escapeEscape the escape of the first block end

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test("PPPTbeforeDstillBeforeTstillBeforeSENDafter"))

-

-    // with blockstart, but escape it so it's not really a block.

-    assertEquals(Some(("ETbeforePPP", "D", "afterNstillafter")), test("PPPETbeforePPPDafterNstillafter"))

-

-    assertEquals(Some(("beforeD", "D", "after")), test("PPPTbeforeDNDafter"))

-

-    // here it would appear that the additional padding to the right of the block end

-    // invalidates the escape scheme as we were only expecting padding to the left of block start

-    assertEquals(Some(("Tbefore", "D", "NPPPDafter")), test("PPPTbeforeDNPPPDafter"))

-

-    val cp2 = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delims, TextStringJustification.Right)

-    def test2(x: String) = x match {

-      case cp2(before, delim, after, null, null, null) => {

-        Some((before, delim, after))

-      }

-      case cp2(null, null, null, before, delim, after) => {

-        Some((before, delim, after))

-      }

-      case z => {

-        // println("no match: " + z); 

-        None

-      }

-    }

-    // This particular regex lets you escape either the blockstart or blockend, or the delimiter

-

-    // no blockstart/end

-    assertEquals(Some(("beforePPP", "D", "after")), test2("PPPbeforePPPDafter"))

-

-    // no blockstart/end, but escape the delimiter

-    assertEquals(Some(("beforeEDstillBeforePPP", "D", "after")), test2("PPPbeforeEDstillBeforePPPDafter"))

-

-    // with blockstart/end

-    assertEquals(Some(("beforeDstillBefore", "D", "after")), test2("PPPTbeforeDstillBeforeNDafter"))

-

-    // with blockstart/end and esc and escEsc found inside (where they are inactive)

-    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test2("PPPTbeforeEDstillBeforeSEDstillBeforeNDafter"))

-    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.

-

-    // with blockstart/end, escape the first block end

-    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test2("PPPTbeforeDstillBeforeENstillBeforeNDafter"))

-

-    // with blockstart/end, escapeEscape the escape of the first block end

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENDafter"))

-

-    // with blockstart, but escape it so it's not really a block.

-    assertEquals(Some(("ETbeforePPP", "D", "afterNstillafter")), test2("PPPETbeforePPPDafterNstillafter"))

-

-    assertEquals(Some(("beforeD", "D", "after")), test2("PPPTbeforeDNDafter"))

-

-    // here it would appear that the additional padding to the right of the block end

-    // invalidates the escape scheme as we were only expecting padding to the left of block start

-    assertEquals(Some(("Tbefore", "D", "NPPPDafter")), test2("PPPTbeforeDNPPPDafter"))

-

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "C", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENCafter"))

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "F", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENFafter"))

-  }

-

-  @Test def testEscapeBlockRegExCenterJustified = {

-    val cp = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delim, TextStringJustification.Center)

-    def test(x: String) = x match {

-      case cp(before, delim, after, null, null, null) => {

-        Some((before, delim, after))

-      }

-      case cp(null, null, null, before, delim, after) => {

-        Some((before, delim, after))

-      }

-      case z => {

-        // println("no match: " + z); 

-        None

-      }

-    }

-    // This particular regex lets you escape either the blockstart or blockend, or the delimiter

-

-    // no blockstart/end

-    assertEquals(Some(("before", "D", "after")), test("PPPbeforePPPDafter"))

-

-    // no blockstart/end, but escape the delimiter

-    assertEquals(Some(("beforeEDstillBefore", "D", "after")), test("PPPbeforeEDstillBeforePPPDafter"))

-

-    // with blockstart/end

-    assertEquals(Some(("beforeDstillBefore", "D", "after")), test("PPPTbeforeDstillBeforeNPPPDafter"))

-

-    // with blockstart/end and esc and escEsc found inside (where they are inactive)

-    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test("PPPTbeforeEDstillBeforeSEDstillBeforeNPPPDafter"))

-    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.

-

-    // with blockstart/end, escape the first block end

-    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test("PPPTbeforeDstillBeforeENstillBeforeNPPPDafter"))

-

-    // with blockstart/end, escapeEscape the escape of the first block end

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test("PPPTbeforeDstillBeforeTstillBeforeSENPPPDafter"))

-

-    // with blockstart, but escape it so it's not really a block.

-    assertEquals(Some(("ETbefore", "D", "afterNstillafter")), test("PPPETbeforePPPDafterNstillafter"))

-

-    // Multiple Delimiters

-    val cp2 = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delims, TextStringJustification.Center)

-    def test2(x: String) = x match {

-      case cp2(before, delim, after, null, null, null) => {

-        Some((before, delim, after))

-      }

-      case cp2(null, null, null, before, delim, after) => {

-        Some((before, delim, after))

-      }

-      case z => {

-        // println("no match: " + z); 

-        None

-      }

-    }

-    // This particular regex lets you escape either the blockstart or blockend, or the delimiter

-

-    // no blockstart/end

-    assertEquals(Some(("before", "D", "after")), test2("PPPbeforePPPDafter"))

-

-    // no blockstart/end, but escape the delimiter

-    assertEquals(Some(("beforeEDstillBefore", "D", "after")), test2("PPPbeforeEDstillBeforePPPDafter"))

-

-    // with blockstart/end

-    assertEquals(Some(("beforeDstillBefore", "D", "after")), test2("PPPTbeforeDstillBeforeNPPPDafter"))

-

-    // with blockstart/end and esc and escEsc found inside (where they are inactive)

-    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test2("PPPTbeforeEDstillBeforeSEDstillBeforeNPPPDafter"))

-    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.

-

-    // with blockstart/end, escape the first block end

-    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test2("PPPTbeforeDstillBeforeENstillBeforeNPPPDafter"))

-

-    // with blockstart/end, escapeEscape the escape of the first block end

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENPPPDafter"))

-

-    // with blockstart, but escape it so it's not really a block.

-    assertEquals(Some(("ETbefore", "D", "afterNstillafter")), test2("PPPETbeforePPPDafterNstillafter"))

-

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "C", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENPPPCafter"))

-    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "F", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENPPPFafter"))

-  }

+
+import junit.framework.Assert._
+import scala.util.parsing.combinator._
+import java.io.StringReader
+import org.junit.Test
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+
+import edu.illinois.ncsa.daffodil.processors.DFDLRegularExpressions
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification
+
+/**
+ * The purpose of this class is to verify that the configurable
+ * nature of the regular expressions does not negatively affect
+ * the results.  Also verifies that the expressions still work
+ * when multiple delimiters are used.
+ */
+class TestDFDLRegularExpressions {
+
+  def escape = "E"
+  def escapeEscape = "S"
+  def delim = """\_*D\_*"""
+  def padChar = "P"
+  def bStart = "T"
+  def bEnd = "N"
+
+  def delims = "D|C|F"
+
+  @Test def testSameEscapeRegExNoPadding = {
+    val cp = DFDLRegularExpressions.getSameEscapeRegEx(escape, delim)
+    def test(x: String) = x match {
+      case cp(before, rubbish, delim, after) => {
+        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)
+        Some((before, delim))
+      }
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test("beforeDafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test("beforeEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test("beforeEEDstillBeforeDafter"))
+    assertEquals(None, test("beforeE"))
+    assertEquals(Some("PPPbeforeEDstillBeforePPP", "D"), test("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforeDafter"))
+    assertEquals(Some("beforeEEEDstillBefore", "D"), test("beforeEEEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test("beforeEEEEDafter"))
+
+    // What about multiple delimiters?
+    val cp2 = DFDLRegularExpressions.getSameEscapeRegEx(escape, delims)
+    def test2(x: String) = x match {
+      case cp2(before, rubbish, delim, after) => {
+        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)
+        Some((before, delim))
+      }
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test2("beforeDafter"))
+    assertEquals(Some("before", "C"), test2("beforeCafter"))
+    assertEquals(Some("before", "F"), test2("beforeFafter"))
+    assertEquals(None, test2("beforeGafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test2("beforeEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test2("beforeEEDstillBeforeDafter"))
+    assertEquals(None, test2("beforeE"))
+    assertEquals(Some("PPPbeforeEDstillBeforePPP", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforeDafter"))
+    assertEquals(Some("beforeEEEDstillBefore", "D"), test2("beforeEEEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test2("beforeEEEEDafter"))
+  }
+
+  @Test def testSameEscapeRegExLeftJustified = {
+    val cp = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delim, padChar, TextStringJustification.Left)
+    def test(x: String) = x match {
+      case cp(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {
+        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)
+        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad
+        Some((before, delim1))
+      }
+      case cp(before, rubbish, delim, null, null, after) => {
+        Some((before, delim))
+      }
+      case cp(before, null, null, ee, delim, after) => {
+        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)
+        Some((before, delim))
+      }
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test("beforeDafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test("beforeEDstillBeforePPPDafter"))
+    assertEquals(Some("before", "D"), test("beforeEEDstillBeforeDafter"))
+    assertEquals(None, test("beforeE"))
+    assertEquals(Some("PPPbeforeEDstillBefore", "D"), test("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforeDafter"))
+    assertEquals(Some("beforeEEEDstillBefore", "D"), test("beforeEEEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test("beforeEEEEDafter"))
+
+    // What about multiple delimiters?
+    val cp2 = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delims, padChar, TextStringJustification.Left)
+    def test2(x: String) = x match {
+      case cp2(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {
+        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)
+        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad
+        Some((before, delim1))
+      }
+      case cp2(before, rubbish, delim, null, null, after) => {
+        Some((before, delim))
+      }
+      case cp2(before, null, null, ee, delim, after) => {
+        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)
+        Some((before, delim))
+      }
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test2("beforeDafter"))
+    assertEquals(Some("before", "C"), test2("beforeCafter"))
+    assertEquals(Some("before", "F"), test2("beforeFafter"))
+    assertEquals(None, test2("beforeGafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test2("beforeEDstillBeforePPPDafter"))
+    assertEquals(Some("before", "D"), test2("beforeEEDstillBeforeDafter"))
+    assertEquals(None, test("beforeE"))
+    assertEquals(Some("PPPbeforeEDstillBefore", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforeDafter"))
+    assertEquals(Some("beforeEEEDstillBefore", "D"), test2("beforeEEEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test2("beforeEEEEDafter"))
+  }
+
+  @Test def testSameEscapeRegExRightJustified = {
+    val cp = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delim, padChar, TextStringJustification.Right)
+    def test(x: String) = x match {
+      case cp(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {
+        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)
+        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad
+        Some((before, delim1))
+      }
+      case cp(before, rubbish, delim, null, null, after) => {
+        Some((before, delim))
+      }
+      case cp(before, null, null, ee, delim, after) => {
+        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)
+        Some((before, delim))
+      }
+      case cp(rubbish, before, rubbish2, delim, after) => {
+        Some((before, delim))
+      }
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test("beforeDafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test("PPPbeforeEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test("PPPbeforeEEDstillBeforeDafter"))
+    assertEquals(None, test("beforeE"))
+    assertEquals(Some("beforeEDstillBeforePPP", "D"), test("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforePPPDafter"))
+    assertEquals(Some("beforeEEEDstillBefore", "D"), test("beforeEEEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test("beforeEEEEDafter"))
+
+    // What about multiple delimiters?
+    val cp2 = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delims, padChar, TextStringJustification.Right)
+    def test2(x: String) = x match {
+      case cp2(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {
+        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)
+        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad
+        Some((before, delim1))
+      }
+      case cp2(before, rubbish, delim, null, null, after) => {
+        Some((before, delim))
+      }
+      case cp2(before, null, null, ee, delim, after) => {
+        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)
+        Some((before, delim))
+      }
+      case cp2(rubbish, before, rubbish2, delim, after) => {
+        Some((before, delim))
+      }
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test2("beforeDafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test2("PPPbeforeEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test2("PPPbeforeEEDstillBeforeDafter"))
+    assertEquals(None, test2("beforeE"))
+    assertEquals(Some("beforeEDstillBeforePPP", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforePPPDafter"))
+    assertEquals(Some("beforeEEEDstillBefore", "D"), test2("beforeEEEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test2("beforeEEEEDafter"))
+  }
+
+  @Test def testSameEscapeRegExCenterJustified = {
+    val cp = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delim, padChar, TextStringJustification.Center)
+    def test(x: String) = x match {
+      case cp(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {
+        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)
+        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad
+        Some((before, delim1))
+      }
+      case cp(before, rubbish, delim, null, null, after) => {
+        Some((before, delim))
+      }
+      case cp(before, null, null, ee, delim, after) => {
+        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)
+        Some((before, delim))
+      }
+      case cp(rubbish, before, rubbish2, delim, after) => {
+        Some((before, delim))
+      }
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test("beforeDafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforePPP", "D"), test("PPPbeforePPPEEDafter"))
+    assertEquals(None, test("beforeE"))
+    assertEquals(Some("beforePPPEDstillBefore", "D"), test("PPPbeforePPPEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforePPPDafter"))
+    assertEquals(Some("beforeEEEDstillBefore", "D"), test("beforeEEEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test("beforeEEEEDafter"))
+
+    // What about multiple delimiters?
+    val cp2 = DFDLRegularExpressions.getSameEscapeRegExWithPadding(escape, delims, padChar, TextStringJustification.Center)
+    def test2(x: String) = x match {
+      case cp2(ee1s, before, ee2s, delimAfterPad, ee3s, delimAfterEEs, after) => {
+        //System.err.println(before + ", " + rubbish + ", " + delim + ", " + after)
+        val delim1 = if (delimAfterPad == null) delimAfterEEs else delimAfterPad
+        Some((before, delim1))
+      }
+      case cp2(before, rubbish, delim, null, null, after) => {
+        Some((before, delim))
+      }
+      case cp2(before, null, null, ee, delim, after) => {
+        //System.err.println(a + ", " + b + ", " + c + "," + d + ", " + e + ", " + f)
+        Some((before, delim))
+      }
+      case cp2(rubbish, before, rubbish2, delim, after) => {
+        Some((before, delim))
+      }
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test2("beforeDafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforePPP", "D"), test2("PPPbeforePPPEEDafter"))
+    assertEquals(None, test("beforeE"))
+    assertEquals(Some("beforePPPEDstillBefore", "D"), test2("PPPbeforePPPEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforePPPDafter"))
+    assertEquals(Some("beforeEEEDstillBefore", "D"), test2("beforeEEEDstillBeforeDafter"))
+    assertEquals(Some("before", "D"), test2("beforeEEEEDafter"))
+  }
+
+  @Test def testEscapeRegExNoPadding = {
+    val cp = DFDLRegularExpressions.getEscapeRegEx(escape, escapeEscape, delim)
+
+    def test(x: String) = x match {
+      case cp(before, delim, blah, after) => Some((before, delim))
+      case cp(before, delim, after) => Some((before, delim))
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test("beforeDafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test("beforeEDstillBeforeDafter"))
+    assertEquals(Some("beforeSE", "D"), test("beforeSEDstillBeforeDafter"))
+    assertEquals(Some("beforeEEDstillBefore", "D"), test("beforeEEDstillBeforeDafter"))
+    assertEquals(None, test("beforeE"))
+    assertEquals(Some("PPPbeforeEDstillBeforePPP", "D"), test("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test("beforeSDstillBeforeDafter"))
+
+    // Multiple delimiters
+    val cp2 = DFDLRegularExpressions.getEscapeRegEx(escape, escapeEscape, delims)
+
+    def test2(x: String) = x match {
+      case cp2(before, delim, blah, after) => Some((before, delim))
+      case cp2(before, delim, after) => Some((before, delim))
+      case z => None
+    }
+    assertEquals(Some("before", "D"), test2("beforeDafter"))
+    assertEquals(Some("beforeEDstillBefore", "D"), test2("beforeEDstillBeforeDafter"))
+    assertEquals(Some("beforeSE", "D"), test2("beforeSEDstillBeforeDafter"))
+    assertEquals(Some("beforeEEDstillBefore", "D"), test2("beforeEEDstillBeforeDafter"))
+    assertEquals(None, test2("beforeE"))
+    assertEquals(Some("PPPbeforeEDstillBeforePPP", "D"), test2("PPPbeforeEDstillBeforePPPDafter"))
+    assertEquals(Some("beforeS", "D"), test2("beforeSDstillBeforeDafter"))
+  }
+
+  @Test def testEscapeRegExLeftJustified = {
+    val cp = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delim, padChar, TextStringJustification.Left)
+    def test(x: String) = x match {
+      case cp(before, delim, after) => Some((before, delim))
+      case z => None
+    }
+    assertEquals(Some(("PPPbefore", "D")), test("PPPbeforePPPDafter"))
+    assertEquals(Some(("PPPbefore", "D")), test("PPPbeforeDafter"))
+    assertEquals(Some(("before", "D")), test("beforeDafter"))
+    assertEquals(None, test("PPPbeforeEDafter"))
+
+    // Multiple Delimiters
+    val cp2 = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delims, padChar, TextStringJustification.Left)
+    def test2(x: String) = x match {
+      case cp2(before, delim, after) => Some((before, delim))
+      case z => None
+    }
+    assertEquals(Some(("PPPbefore", "D")), test2("PPPbeforePPPDafter"))
+    assertEquals(Some(("PPPbefore", "D")), test2("PPPbeforeDafter"))
+    assertEquals(Some(("before", "D")), test2("beforeDafter"))
+    assertEquals(None, test2("PPPbeforeEDafter"))
+    assertEquals(Some(("before", "C")), test2("beforeCafter"))
+    assertEquals(Some(("before", "F")), test2("beforeFafter"))
+  }
+
+  @Test def testEscapeRegExRightJustified = {
+    val cp = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delim, padChar, TextStringJustification.Right)
+    def test(x: String) = x match {
+      case cp(before, delim, after) => Some((before, delim))
+      case z => None
+    }
+    assertEquals(Some(("beforePPP", "D")), test("PPPbeforePPPDafter"))
+    assertEquals(Some(("beforePPP", "D")), test("beforePPPDafter"))
+    assertEquals(Some(("before", "D")), test("beforeDafter"))
+    assertEquals(None, test("PPPbeforeEDafter"))
+
+    // Multiple Delimiters
+    val cp2 = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delims, padChar, TextStringJustification.Right)
+    def test2(x: String) = x match {
+      case cp2(before, delim, after) => Some((before, delim))
+      case z => None
+    }
+    assertEquals(Some(("beforePPP", "D")), test2("PPPbeforePPPDafter"))
+    assertEquals(Some(("beforePPP", "D")), test2("beforePPPDafter"))
+    assertEquals(Some(("before", "D")), test2("beforeDafter"))
+    assertEquals(None, test("PPPbeforeEDafter"))
+    assertEquals(Some(("before", "C")), test2("beforeCafter"))
+    assertEquals(Some(("before", "F")), test2("beforeFafter"))
+  }
+
+  @Test def testEscapeRegExCenterJustified = {
+    val cp = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delim, padChar, TextStringJustification.Center)
+    def test(x: String) = x match {
+      case cp(before, delim, after) => Some((before, delim))
+      case z => None
+    }
+    assertEquals(Some(("beforePPPEDmore", "D")), test("PPPbeforePPPEDmoreDafter"))
+    //assertEquals(Some(("beforePPPEDmoreEP", "D")), test("PPPbeforePPPEDmoreEPPPDafter"))
+    assertEquals(Some(("before", "D")), test("PPPbeforePPPDafter"))
+    assertEquals(Some(("before", "D")), test("beforeDafter"))
+    assertEquals(Some(("before", "D")), test("beforePPPDafter"))
+    assertEquals(Some(("before", "D")), test("PPPbeforeDafter"))
+    assertEquals(None, test("PPPbeforeEDafter"))
+
+    // Multiple Delimiters
+    val cp2 = DFDLRegularExpressions.getEscapeRegExWithPadding(escape, escapeEscape, delims, padChar, TextStringJustification.Center)
+    def test2(x: String) = x match {
+      case cp2(before, delim, after) => Some((before, delim))
+      case z => None
+    }
+    assertEquals(Some(("beforePPPEDmore", "D")), test2("PPPbeforePPPEDmoreDafter"))
+    //assertEquals(Some(("beforePPPEDmoreEP", "D")), test("PPPbeforePPPEDmoreEPPPDafter"))
+    assertEquals(Some(("before", "D")), test2("PPPbeforePPPDafter"))
+    assertEquals(Some(("before", "D")), test2("beforeDafter"))
+    assertEquals(Some(("before", "D")), test2("beforePPPDafter"))
+    assertEquals(Some(("before", "D")), test2("PPPbeforeDafter"))
+    assertEquals(None, test2("PPPbeforeEDafter"))
+    assertEquals(Some(("before", "C")), test2("beforeCafter"))
+    assertEquals(Some(("before", "F")), test2("beforeFafter"))
+  }
+
+  @Test def testEscapeBlockRegExNoPadding = {
+    val cp = DFDLRegularExpressions.getEscapeBlockRegEx(bStart, bEnd, escapeEscape, escape, delim)
+    def test(x: String) = x match {
+      case cp(before, delim, after, null, null, null) => {
+        Some((before, delim, after))
+      }
+      case cp(null, null, null, before, delim, after) => {
+        Some((before, delim, after))
+      }
+      case z => {
+        // println("no match: " + z); 
+        None
+      }
+    }
+    // This particular regex lets you escape either the blockstart or blockend, or the delimiter
+
+    // no blockstart/end
+    assertEquals(Some(("before", "D", "after")), test("beforeDafter"))
+
+    // no blockstart/end, but escape the delimiter
+    assertEquals(Some(("beforeEDstillBefore", "D", "after")), test("beforeEDstillBeforeDafter"))
+
+    // with blockstart/end
+    assertEquals(Some(("beforeDstillBefore", "D", "after")), test("TbeforeDstillBeforeNDafter"))
+
+    // with blockstart/end and esc and escEsc found inside (where they are inactive)
+    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test("TbeforeEDstillBeforeSEDstillBeforeNDafter"))
+    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.
+
+    // with blockstart/end, escape the first block end
+    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test("TbeforeDstillBeforeENstillBeforeNDafter"))
+
+    // with blockstart/end, escapeEscape the escape of the first block end
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test("TbeforeDstillBeforeTstillBeforeSENDafter"))
+
+    // with blockstart, but escape it so it's not really a block.
+    assertEquals(Some(("ETbefore", "D", "afterNstillafter")), test("ETbeforeDafterNstillafter"))
+
+    // Multiple delimiters
+    val cp2 = DFDLRegularExpressions.getEscapeBlockRegEx(bStart, bEnd, escapeEscape, escape, delims)
+    def test2(x: String) = x match {
+      case cp2(before, delim, after, null, null, null) => {
+        Some((before, delim, after))
+      }
+      case cp2(null, null, null, before, delim, after) => {
+        Some((before, delim, after))
+      }
+      case z => {
+        // println("no match: " + z); 
+        None
+      }
+    }
+    // This particular regex lets you escape either the blockstart or blockend, or the delimiter
+
+    // no blockstart/end
+    assertEquals(Some(("before", "D", "after")), test2("beforeDafter"))
+
+    // no blockstart/end, but escape the delimiter
+    assertEquals(Some(("beforeEDstillBefore", "D", "after")), test2("beforeEDstillBeforeDafter"))
+
+    // with blockstart/end
+    assertEquals(Some(("beforeDstillBefore", "D", "after")), test2("TbeforeDstillBeforeNDafter"))
+
+    // with blockstart/end and esc and escEsc found inside (where they are inactive)
+    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test2("TbeforeEDstillBeforeSEDstillBeforeNDafter"))
+    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.
+
+    // with blockstart/end, escape the first block end
+    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test2("TbeforeDstillBeforeENstillBeforeNDafter"))
+
+    // with blockstart/end, escapeEscape the escape of the first block end
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test2("TbeforeDstillBeforeTstillBeforeSENDafter"))
+
+    // with blockstart, but escape it so it's not really a block.
+    assertEquals(Some(("ETbefore", "D", "afterNstillafter")), test2("ETbeforeDafterNstillafter"))
+
+    assertEquals(Some(("beforeDstillBeforeENstillBefore", "C", "after")), test2("TbeforeDstillBeforeENstillBeforeNCafter"))
+    assertEquals(Some(("beforeDstillBeforeENstillBefore", "F", "after")), test2("TbeforeDstillBeforeENstillBeforeNFafter"))
+  }
+
+  @Test def testEscapeBlockRegExLeftJustified = {
+    val cp = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delim, TextStringJustification.Left)
+    def test(x: String) = x match {
+      case cp(before, delim, after, null, null, null) => {
+        Some((before, delim, after))
+      }
+      case cp(null, null, null, before, delim, after) => {
+        Some((before, delim, after))
+      }
+      case z => {
+        // println("no match: " + z); 
+        None
+      }
+    }
+    // This particular regex lets you escape either the blockstart or blockend, or the delimiter
+
+    // Because this is Left justified the padding at the start of the field is actually
+    // considered to be content.  This breaks the normal behavior of the blockstart and blockend
+    // because it is required that blockstart must occur at the very beginning of the content. 
+
+    // no blockstart/end
+    assertEquals(Some(("PPPbefore", "D", "after")), test("PPPbeforePPPDafter"))
+
+    // no blockstart/end, but escape the delimiter
+    assertEquals(Some(("PPPbeforeEDstillBefore", "D", "after")), test("PPPbeforeEDstillBeforePPPDafter"))
+
+    // with blockstart/end
+    assertEquals(Some(("PPPTbefore", "D", "stillBeforeNPPPDafter")), test("PPPTbeforeDstillBeforeNPPPDafter"))
+
+    // with blockstart/end and esc and escEsc found inside (where they are inactive)
+    assertEquals(Some(("PPPTbeforeEDstillBeforeSE", "D", "stillBeforeNPPPDafter")), test("PPPTbeforeEDstillBeforeSEDstillBeforeNPPPDafter"))
+    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.
+
+    // with blockstart/end, escape the first block end
+    assertEquals(Some(("PPPTbefore", "D", "stillBeforeENstillBeforeNPPPDafter")), test("PPPTbeforeDstillBeforeENstillBeforeNPPPDafter"))
+
+    // with blockstart/end, escapeEscape the escape of the first block end
+    assertEquals(Some(("PPPTbefore", "D", "stillBeforeTstillBeforeSENPPPDafter")), test("PPPTbeforeDstillBeforeTstillBeforeSENPPPDafter"))
+
+    // with blockstart, but escape it so it's not really a block.
+    assertEquals(Some(("PPPETbefore", "D", "afterNstillafter")), test("PPPETbeforePPPDafterNstillafter"))
+
+    // here it would appear that the additional padding to the left of the block start
+    // invalidates the escape scheme as we were only expecting padding to the right of block end
+    assertEquals(Some(("PPPTbefore", "D", "NPPPDafter")), test("PPPTbeforeDNPPPDafter"))
+
+    // Multiple Delimiters
+    val cp2 = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delims, TextStringJustification.Left)
+    def test2(x: String) = x match {
+      case cp2(before, delim, after, null, null, null) => {
+        Some((before, delim, after))
+      }
+      case cp2(null, null, null, before, delim, after) => {
+        Some((before, delim, after))
+      }
+      case z => {
+        // println("no match: " + z); 
+        None
+      }
+    }
+    // This particular regex lets you escape either the blockstart or blockend, or the delimiter
+
+    // Because this is Left justified the padding at the start of the field is actually
+    // considered to be content.  This breaks the normal behavior of the blockstart and blockend
+    // because it is required that blockstart must occur at the very beginning of the content. 
+
+    // no blockstart/end
+    assertEquals(Some(("PPPbefore", "D", "after")), test2("PPPbeforePPPDafter"))
+
+    // no blockstart/end, but escape the delimiter
+    assertEquals(Some(("PPPbeforeEDstillBefore", "D", "after")), test2("PPPbeforeEDstillBeforePPPDafter"))
+
+    // with blockstart/end
+    assertEquals(Some(("PPPTbefore", "D", "stillBeforeNPPPDafter")), test2("PPPTbeforeDstillBeforeNPPPDafter"))
+
+    // with blockstart/end and esc and escEsc found inside (where they are inactive)
+    assertEquals(Some(("PPPTbeforeEDstillBeforeSE", "D", "stillBeforeNPPPDafter")), test2("PPPTbeforeEDstillBeforeSEDstillBeforeNPPPDafter"))
+    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.
+
+    // with blockstart/end, escape the first block end
+    assertEquals(Some(("PPPTbefore", "D", "stillBeforeENstillBeforeNPPPDafter")), test2("PPPTbeforeDstillBeforeENstillBeforeNPPPDafter"))
+
+    // with blockstart/end, escapeEscape the escape of the first block end
+    assertEquals(Some(("PPPTbefore", "D", "stillBeforeTstillBeforeSENPPPDafter")), test2("PPPTbeforeDstillBeforeTstillBeforeSENPPPDafter"))
+
+    // with blockstart, but escape it so it's not really a block.
+    assertEquals(Some(("PPPETbefore", "D", "afterNstillafter")), test2("PPPETbeforePPPDafterNstillafter"))
+
+    // here it would appear that the additional padding to the left of the block start
+    // invalidates the escape scheme as we were only expecting padding to the right of block end
+    assertEquals(Some(("PPPTbefore", "D", "NPPPDafter")), test2("PPPTbeforeDNPPPDafter"))
+
+    assertEquals(Some(("PPPTbefore", "C", "stillBeforeTstillBeforeSENPPPDafter")), test2("PPPTbeforeCstillBeforeTstillBeforeSENPPPDafter"))
+    assertEquals(Some(("PPPTbefore", "F", "stillBeforeTstillBeforeSENPPPDafter")), test2("PPPTbeforeFstillBeforeTstillBeforeSENPPPDafter"))
+  }
+
+  @Test def testEscapeBlockRegExRightJustified = {
+    val cp = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delim, TextStringJustification.Right)
+    def test(x: String) = x match {
+      case cp(before, delim, after, null, null, null) => {
+        Some((before, delim, after))
+      }
+      case cp(null, null, null, before, delim, after) => {
+        Some((before, delim, after))
+      }
+      case z => {
+        // println("no match: " + z); 
+        None
+      }
+    }
+    // This particular regex lets you escape either the blockstart or blockend, or the delimiter
+
+    // no blockstart/end
+    assertEquals(Some(("beforePPP", "D", "after")), test("PPPbeforePPPDafter"))
+
+    // no blockstart/end, but escape the delimiter
+    assertEquals(Some(("beforeEDstillBeforePPP", "D", "after")), test("PPPbeforeEDstillBeforePPPDafter"))
+
+    // with blockstart/end
+    assertEquals(Some(("beforeDstillBefore", "D", "after")), test("PPPTbeforeDstillBeforeNDafter"))
+
+    // with blockstart/end and esc and escEsc found inside (where they are inactive)
+    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test("PPPTbeforeEDstillBeforeSEDstillBeforeNDafter"))
+    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.
+
+    // with blockstart/end, escape the first block end
+    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test("PPPTbeforeDstillBeforeENstillBeforeNDafter"))
+
+    // with blockstart/end, escapeEscape the escape of the first block end
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test("PPPTbeforeDstillBeforeTstillBeforeSENDafter"))
+
+    // with blockstart, but escape it so it's not really a block.
+    assertEquals(Some(("ETbeforePPP", "D", "afterNstillafter")), test("PPPETbeforePPPDafterNstillafter"))
+
+    assertEquals(Some(("beforeD", "D", "after")), test("PPPTbeforeDNDafter"))
+
+    // here it would appear that the additional padding to the right of the block end
+    // invalidates the escape scheme as we were only expecting padding to the left of block start
+    assertEquals(Some(("Tbefore", "D", "NPPPDafter")), test("PPPTbeforeDNPPPDafter"))
+
+    val cp2 = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delims, TextStringJustification.Right)
+    def test2(x: String) = x match {
+      case cp2(before, delim, after, null, null, null) => {
+        Some((before, delim, after))
+      }
+      case cp2(null, null, null, before, delim, after) => {
+        Some((before, delim, after))
+      }
+      case z => {
+        // println("no match: " + z); 
+        None
+      }
+    }
+    // This particular regex lets you escape either the blockstart or blockend, or the delimiter
+
+    // no blockstart/end
+    assertEquals(Some(("beforePPP", "D", "after")), test2("PPPbeforePPPDafter"))
+
+    // no blockstart/end, but escape the delimiter
+    assertEquals(Some(("beforeEDstillBeforePPP", "D", "after")), test2("PPPbeforeEDstillBeforePPPDafter"))
+
+    // with blockstart/end
+    assertEquals(Some(("beforeDstillBefore", "D", "after")), test2("PPPTbeforeDstillBeforeNDafter"))
+
+    // with blockstart/end and esc and escEsc found inside (where they are inactive)
+    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test2("PPPTbeforeEDstillBeforeSEDstillBeforeNDafter"))
+    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.
+
+    // with blockstart/end, escape the first block end
+    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test2("PPPTbeforeDstillBeforeENstillBeforeNDafter"))
+
+    // with blockstart/end, escapeEscape the escape of the first block end
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENDafter"))
+
+    // with blockstart, but escape it so it's not really a block.
+    assertEquals(Some(("ETbeforePPP", "D", "afterNstillafter")), test2("PPPETbeforePPPDafterNstillafter"))
+
+    assertEquals(Some(("beforeD", "D", "after")), test2("PPPTbeforeDNDafter"))
+
+    // here it would appear that the additional padding to the right of the block end
+    // invalidates the escape scheme as we were only expecting padding to the left of block start
+    assertEquals(Some(("Tbefore", "D", "NPPPDafter")), test2("PPPTbeforeDNPPPDafter"))
+
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "C", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENCafter"))
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "F", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENFafter"))
+  }
+
+  @Test def testEscapeBlockRegExCenterJustified = {
+    val cp = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delim, TextStringJustification.Center)
+    def test(x: String) = x match {
+      case cp(before, delim, after, null, null, null) => {
+        Some((before, delim, after))
+      }
+      case cp(null, null, null, before, delim, after) => {
+        Some((before, delim, after))
+      }
+      case z => {
+        // println("no match: " + z); 
+        None
+      }
+    }
+    // This particular regex lets you escape either the blockstart or blockend, or the delimiter
+
+    // no blockstart/end
+    assertEquals(Some(("before", "D", "after")), test("PPPbeforePPPDafter"))
+
+    // no blockstart/end, but escape the delimiter
+    assertEquals(Some(("beforeEDstillBefore", "D", "after")), test("PPPbeforeEDstillBeforePPPDafter"))
+
+    // with blockstart/end
+    assertEquals(Some(("beforeDstillBefore", "D", "after")), test("PPPTbeforeDstillBeforeNPPPDafter"))
+
+    // with blockstart/end and esc and escEsc found inside (where they are inactive)
+    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test("PPPTbeforeEDstillBeforeSEDstillBeforeNPPPDafter"))
+    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.
+
+    // with blockstart/end, escape the first block end
+    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test("PPPTbeforeDstillBeforeENstillBeforeNPPPDafter"))
+
+    // with blockstart/end, escapeEscape the escape of the first block end
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test("PPPTbeforeDstillBeforeTstillBeforeSENPPPDafter"))
+
+    // with blockstart, but escape it so it's not really a block.
+    assertEquals(Some(("ETbefore", "D", "afterNstillafter")), test("PPPETbeforePPPDafterNstillafter"))
+
+    // Multiple Delimiters
+    val cp2 = DFDLRegularExpressions.getEscapeBlockRegExWithPadding(bStart, bEnd, escapeEscape, escape, padChar, delims, TextStringJustification.Center)
+    def test2(x: String) = x match {
+      case cp2(before, delim, after, null, null, null) => {
+        Some((before, delim, after))
+      }
+      case cp2(null, null, null, before, delim, after) => {
+        Some((before, delim, after))
+      }
+      case z => {
+        // println("no match: " + z); 
+        None
+      }
+    }
+    // This particular regex lets you escape either the blockstart or blockend, or the delimiter
+
+    // no blockstart/end
+    assertEquals(Some(("before", "D", "after")), test2("PPPbeforePPPDafter"))
+
+    // no blockstart/end, but escape the delimiter
+    assertEquals(Some(("beforeEDstillBefore", "D", "after")), test2("PPPbeforeEDstillBeforePPPDafter"))
+
+    // with blockstart/end
+    assertEquals(Some(("beforeDstillBefore", "D", "after")), test2("PPPTbeforeDstillBeforeNPPPDafter"))
+
+    // with blockstart/end and esc and escEsc found inside (where they are inactive)
+    assertEquals(Some(("beforeEDstillBeforeSEDstillBefore", "D", "after")), test2("PPPTbeforeEDstillBeforeSEDstillBeforeNPPPDafter"))
+    // Note: in the above, the SED is ok. No postprocessing. It is escaped in entirety by the T---N pair.
+
+    // with blockstart/end, escape the first block end
+    assertEquals(Some(("beforeDstillBeforeENstillBefore", "D", "after")), test2("PPPTbeforeDstillBeforeENstillBeforeNPPPDafter"))
+
+    // with blockstart/end, escapeEscape the escape of the first block end
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "D", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENPPPDafter"))
+
+    // with blockstart, but escape it so it's not really a block.
+    assertEquals(Some(("ETbefore", "D", "afterNstillafter")), test2("PPPETbeforePPPDafterNstillafter"))
+
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "C", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENPPPCafter"))
+    assertEquals(Some(("beforeDstillBeforeTstillBeforeSE", "F", "after")), test2("PPPTbeforeDstillBeforeTstillBeforeSENPPPFafter"))
+  }
 }
\ No newline at end of file
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/EncodingMixins.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/EncodingMixins.scala
index 0285904..ec1ee36 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/EncodingMixins.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/dsom/EncodingMixins.scala
@@ -1,165 +1,165 @@
-package edu.illinois.ncsa.daffodil.dsom

-

-/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.

- *

- * Developed by: Tresys Technology, LLC

- *               http://www.tresys.com

- * 

- * Permission is hereby granted, free of charge, to any person obtaining a copy of

- * this software and associated documentation files (the "Software"), to deal with

- * the Software without restriction, including without limitation the rights to

- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies

- * of the Software, and to permit persons to whom the Software is furnished to do

- * so, subject to the following conditions:

- * 

- *  1. Redistributions of source code must retain the above copyright notice,

- *     this list of conditions and the following disclaimers.

- * 

- *  2. Redistributions in binary form must reproduce the above copyright

- *     notice, this list of conditions and the following disclaimers in the

- *     documentation and/or other materials provided with the distribution.

- * 

- *  3. Neither the names of Tresys Technology, nor the names of its contributors

- *     may be used to endorse or promote products derived from this Software

- *     without specific prior written permission.

- * 

- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

- * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE

- * SOFTWARE.

- */

-

-import edu.illinois.ncsa.daffodil.processors.charset.CharsetUtils

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.UTF16Width

-

-/**

- * Split this out of AnnotatedMixin for separation of

- * concerns reasons.

- *

- * TODO: move to GrammarMixins.scala, or another file

- * of these sorts of traits that are mixed onto the

- * schema components.

- */

-trait EncodingMixin { self: AnnotatedSchemaComponent =>

-  /**

-   * Character encoding common attributes

-   *

-   * Note that since encoding can be computed at runtime, we

-   * create values to tell us if the encoding is known or not

-   * so that we can decide things at compile time when possible.

-   */

-

-  lazy val isKnownEncoding = {

-    val isKnown = encoding.isConstant

-    if (isKnown) {

-      val encName = encoding.constantAsString.toUpperCase()

-      if (encName.startsWith("UTF-16")) {

-        schemaDefinitionUnless(utf16Width == UTF16Width.Fixed, "Property utf16Width='variable' not supported.")

-        //

-        // TODO: when runtime encoding is supproted, must also check for utf16Width

-        // (and error if unsupported then, or just implement it!)

-      }

-    }

-    isKnown

-  }

-

-  /**

-   * Note that the canonical form for encoding names is all upper case.

-   */

-  lazy val knownEncodingName = {

-    Assert.invariant(isKnownEncoding)

-    val res = encoding.constantAsString.toUpperCase()

-    res

-  }

-

-  lazy val knownEncodingCharset = {

-    CharsetUtils.getCharset(knownEncodingName)

-  }

-

-  // Really bad idea. Don't save these. Decoders and Encoders are stateful

-  // so they can't be precomputed here and reused without all sorts of 

-  // thread issues and reset protocols.

-  //  lazy val knownEncodingDecoder = {

-  //    val decoder = knownEncodingCharset.newDecoder()

-  //    decoder

-  //  }

-  //

-  //  lazy val knownEncodingEncoder = {

-  //    val encoder = knownEncodingCharset.newEncoder()

-  //    encoder

-  //  }

-

-  lazy val knownEncodingAlignmentInBits = {

-    knownEncodingName match {

-      case "US-ASCII-7-BIT-PACKED" => 1 // canonical form of encoding names is all upper case

-      case _ => 8

-    }

-  }

-

-  /**

-   * enables optimizations and random-access

-   *

-   * variable-width character sets require scanning to determine

-   * their end.

-   */

-  lazy val knownEncodingIsFixedWidth = {

-    // val res = knownEncodingCharset.isFixedWidth

-    val res = knownEncodingName match {

-      case "US-ASCII" | "ASCII" => true

-      case "US-ASCII-7-BIT-PACKED" => true

-      case "UTF-8" => false

-      case "UTF-16" | "UTF-16LE" | "UTF-16BE" => {

-        if (utf16Width == UTF16Width.Fixed) true

-        else false

-      }

-      case "UTF-32" | "UTF-32BE" | "UTF-32LE" => true

-      case "ISO-8859-1" => true

-      case _ => schemaDefinitionError("Text encoding '%s' is not supported.", knownEncodingName)

-    }

-    res

-  }

-

-  lazy val couldBeVariableWidthEncoding = !knownEncodingIsFixedWidth

-

-  lazy val knownEncodingWidthInBits = {

-    // knownEncodingCharset.width()

-    val res = knownEncodingName match {

-      case "US-ASCII" | "ASCII" => 8

-      case "US-ASCII-7-BIT-PACKED" => 7 // NOTE! 7-bit characters dense packed. 8th bit is NOT unused. 

-      case "UTF-8" => -1

-      case "UTF-16" | "UTF-16LE" | "UTF-16BE" => {

-        if (utf16Width == UTF16Width.Fixed) 16

-        else -1

-      }

-      case "UTF-32" | "UTF-32BE" | "UTF-32LE" => 32

-      case "ISO-8859-1" => 8

-      case _ => schemaDefinitionError("Text encoding '%s' is not supported.", knownEncodingName)

-    }

-    res

-  }

-

-  lazy val knownEncodingStringBitLengthFunction = {

-    //

-    // This will be called at runtime, so let's decide

-    // what we can, and return an optimized function that 

-    // has characteristics of the encoding wired down.

-    //

-    if (knownEncodingIsFixedWidth) {

-      def stringBitLength(str: String) = str.length * knownEncodingWidthInBits

-      stringBitLength _

-    } else {

-      def stringBitLength(str: String) = {

-        // variable width encoding, so we have to convert each character 

-        // We assume here that it will be a multiple of bytes

-        // that is, that variable-width encodings are all some number

-        // of bytes.

-        str.getBytes(knownEncodingName).length * 8

-      }

-      stringBitLength _

-    }

-  }

-}

+package edu.illinois.ncsa.daffodil.dsom
+
+/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
+ *
+ * Developed by: Tresys Technology, LLC
+ *               http://www.tresys.com
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal with
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ * 
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimers.
+ * 
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimers in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *  3. Neither the names of Tresys Technology, nor the names of its contributors
+ *     may be used to endorse or promote products derived from this Software
+ *     without specific prior written permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
+ * SOFTWARE.
+ */
+
+import edu.illinois.ncsa.daffodil.processors.charset.CharsetUtils
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.UTF16Width
+
+/**
+ * Split this out of AnnotatedMixin for separation of
+ * concerns reasons.
+ *
+ * TODO: move to GrammarMixins.scala, or another file
+ * of these sorts of traits that are mixed onto the
+ * schema components.
+ */
+trait EncodingMixin { self: AnnotatedSchemaComponent =>
+  /**
+   * Character encoding common attributes
+   *
+   * Note that since encoding can be computed at runtime, we
+   * create values to tell us if the encoding is known or not
+   * so that we can decide things at compile time when possible.
+   */
+
+  lazy val isKnownEncoding = {
+    val isKnown = encoding.isConstant
+    if (isKnown) {
+      val encName = encoding.constantAsString.toUpperCase()
+      if (encName.startsWith("UTF-16")) {
+        schemaDefinitionUnless(utf16Width == UTF16Width.Fixed, "Property utf16Width='variable' not supported.")
+        //
+        // TODO: when runtime encoding is supproted, must also check for utf16Width
+        // (and error if unsupported then, or just implement it!)
+      }
+    }
+    isKnown
+  }
+
+  /**
+   * Note that the canonical form for encoding names is all upper case.
+   */
+  lazy val knownEncodingName = {
+    Assert.invariant(isKnownEncoding)
+    val res = encoding.constantAsString.toUpperCase()
+    res
+  }
+
+  lazy val knownEncodingCharset = {
+    CharsetUtils.getCharset(knownEncodingName)
+  }
+
+  // Really bad idea. Don't save these. Decoders and Encoders are stateful
+  // so they can't be precomputed here and reused without all sorts of 
+  // thread issues and reset protocols.
+  //  lazy val knownEncodingDecoder = {
+  //    val decoder = knownEncodingCharset.newDecoder()
+  //    decoder
+  //  }
+  //
+  //  lazy val knownEncodingEncoder = {
+  //    val encoder = knownEncodingCharset.newEncoder()
+  //    encoder
+  //  }
+
+  lazy val knownEncodingAlignmentInBits = {
+    knownEncodingName match {
+      case "US-ASCII-7-BIT-PACKED" => 1 // canonical form of encoding names is all upper case
+      case _ => 8
+    }
+  }
+
+  /**
+   * enables optimizations and random-access
+   *
+   * variable-width character sets require scanning to determine
+   * their end.
+   */
+  lazy val knownEncodingIsFixedWidth = {
+    // val res = knownEncodingCharset.isFixedWidth
+    val res = knownEncodingName match {
+      case "US-ASCII" | "ASCII" => true
+      case "US-ASCII-7-BIT-PACKED" => true
+      case "UTF-8" => false
+      case "UTF-16" | "UTF-16LE" | "UTF-16BE" => {
+        if (utf16Width == UTF16Width.Fixed) true
+        else false
+      }
+      case "UTF-32" | "UTF-32BE" | "UTF-32LE" => true
+      case "ISO-8859-1" => true
+      case _ => schemaDefinitionError("Text encoding '%s' is not supported.", knownEncodingName)
+    }
+    res
+  }
+
+  lazy val couldBeVariableWidthEncoding = !knownEncodingIsFixedWidth
+
+  lazy val knownEncodingWidthInBits = {
+    // knownEncodingCharset.width()
+    val res = knownEncodingName match {
+      case "US-ASCII" | "ASCII" => 8
+      case "US-ASCII-7-BIT-PACKED" => 7 // NOTE! 7-bit characters dense packed. 8th bit is NOT unused. 
+      case "UTF-8" => -1
+      case "UTF-16" | "UTF-16LE" | "UTF-16BE" => {
+        if (utf16Width == UTF16Width.Fixed) 16
+        else -1
+      }
+      case "UTF-32" | "UTF-32BE" | "UTF-32LE" => 32
+      case "ISO-8859-1" => 8
+      case _ => schemaDefinitionError("Text encoding '%s' is not supported.", knownEncodingName)
+    }
+    res
+  }
+
+  lazy val knownEncodingStringBitLengthFunction = {
+    //
+    // This will be called at runtime, so let's decide
+    // what we can, and return an optimized function that 
+    // has characteristics of the encoding wired down.
+    //
+    if (knownEncodingIsFixedWidth) {
+      def stringBitLength(str: String) = str.length * knownEncodingWidthInBits
+      stringBitLength _
+    } else {
+      def stringBitLength(str: String) = {
+        // variable width encoding, so we have to convert each character 
+        // We assume here that it will be a multiple of bytes
+        // that is, that variable-width encodings are all some number
+        // of bytes.
+        str.getBytes(knownEncodingName).length * 8
+      }
+      stringBitLength _
+    }
+  }
+}
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParser.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParser.scala
index 23d382c..c0d3de8 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParser.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParser.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.processors

-

+package edu.illinois.ncsa.daffodil.processors
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,544 +30,544 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import scala.annotation.migration

-import scala.collection.mutable.Queue

-import scala.util.parsing.combinator.RegexParsers

-import scala.util.parsing.input.Reader

-import edu.illinois.ncsa.daffodil.processors.DelimiterType._

-import edu.illinois.ncsa.daffodil.processors.DelimiterLocation._

-import scala.util.matching.Regex

-import java.nio.charset.Charset

-import java.util.regex.Pattern

-import edu.illinois.ncsa.daffodil.util.Logging

-import edu.illinois.ncsa.daffodil.util.LogLevel

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.dsom.AnnotatedSchemaComponent

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import edu.illinois.ncsa.daffodil.util.Info

-

-class DelimParser(stringBitLengthFunction: String => Int) extends DFDLDelimParserCommon(stringBitLengthFunction) {

-

-  def removePadding(input: String, justification: TextJustificationType.Type, padChar: String): String = {

-    if ((padChar.length() == 0)) { return input }

-    val field = this.generateRemovePaddingParser(justification, padChar)

-    val result = field match {

-      case None => input

-      case Some(f) => {

-        val res = this.parse(this.log(f)({

-          justification match {

-            case TextJustificationType.Left => "DelimParser.removePadding.leftJustified"

-            case TextJustificationType.Right => "DelimParser.removePadding.rightJustified"

-            case TextJustificationType.Center => "DelimParser.removePadding.centerJustified"

-            case TextJustificationType.None => Assert.invariantFailed("should not be none if we're trimming.")

-          }

-        }), input)

-        res.getOrElse(input)

-      }

-    }

-    result

-  }

-

-  def parseInputPatterned(pattern: String, input: Reader[Char]): DelimParseResult = {

-    val entry = this.generateInputPatternedParser(pattern)

-

-    // FOR DEBUGGING might want this logging version

-    val res = this.parse(this.log(entry)("DelimParser.parseInputPatterned"), input)

-    //val res = this.parse(entry, input)

-

-    res match {

-      case s @ Success(_, _) => DelimParseSuccessFactory(s, "", DelimiterType.NotDelimited, None, DelimiterLocation.Local)

-      case f: NoSuccess => DelimParseFailure(f.msg, f.next)

-    }

-  }

-

-  def parseInputNCharacters(nChars: Long, input: Reader[Char],

-                            justification: TextJustificationType.Type,

-                            padChar: String): DelimParseResult = {

-

-    val entry = this.generateInputNCharactersParser(nChars)

-

-    // For debug can use this logging parser instead.

-    val res = this.parse(this.log(entry)("DelimParser.parseInputNCharacters"), input)

-    //val res = this.parse(entry, input)

-

-    res match {

-      case s @ Success(field, next) => {

-        val fieldNoPadding = removePadding(field, justification, padChar)

-        DelimParseSuccessFactory(Success(fieldNoPadding, next), "", DelimiterType.NotDelimited, Some(field), DelimiterLocation.Local)

-      }

-      case f: NoSuccess => DelimParseFailure(f.msg, f.next)

-    }

-  }

-

-  //  private def parseInputEscapeCharContent(fieldParser: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],

-  //    input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {

-  //    parseInputDefaultContent(fieldParser, seps, terms, input, justification)

-  //  }

-

-  /**

-   * localDelims - delimiters local to the component in question

-   * remoteDelims - delimiters of an enclosing container of this component

-   *

-   * Assumes that remoteDelims does not contain any String found in localDelims

-   *

-   * The call to buildDelims sorts the delimiters by length or possible length.

-   */

-  def parseInputDelimiter(localDelims: Set[String], remoteDelims: Set[String],

-                          input: Reader[Char]): DelimParseResult = {

-

-    val (localDelimsParser, localDelimsRegex) = generateDelimiter(localDelims)

-    val (remoteDelimsParser, remoteDelimsRegex) = generateDelimiter(remoteDelims)

-    val entry = generateInputDelimiterParser(localDelimsParser, remoteDelimsParser)

-

-    val res = this.parse(this.log(entry)("DelimParser.parseInputDelimiter.allDelims"), input)

-

-    // TODO: This seems pretty inefficient. We're redoing a match in order to know 

-    // whether it was local or remote?? 

-    val result = res match {

-      case s @ Success(delimiterResult, next) => {

-        // We have a result but was it a remote or local match?

-        // We need the regex to match exactly the whole delimiterResult

-        // Here localDelimsRegex should have already been sorted by the buildDelims call

-        // we simply need to tell the regex that it has to match the full delimiterResult String.

-        // If it doesn't match, then that means the match had to be a remote delimiter.

-        val newLocalDelimsRegex = "(?s)^(" + combineDelimitersRegex(localDelimsRegex, Array.empty[String]) + ")$"

-        val newLocalDelimsParser: Parser[String] = newLocalDelimsRegex.r

-

-        val subResult = this.parseAll(this.log(newLocalDelimsParser)("DelimParser.parseInputDelimiter.isLocal"), delimiterResult)

-        val delimiterLocation = if (subResult.isEmpty) DelimiterLocation.Remote else DelimiterLocation.Local

-        //

-        // TODO: ?? Is None the right thing to pass here?? If we pass none, then it is 

-        // going to determine the length based on the delimiterResult. Does that include

-        // everything it needs to include?

-        //

-        DelimParseSuccessFactory(s, delimiterResult, DelimiterType.NotDelimited, None, // Is None right?

-          delimiterLocation)

-      }

-      case NoSuccess(msg, next) => DelimParseFailure(msg, next)

-    }

-    result

-  }

-

-  /**

-   * Parses text input without escape schemes

-   */

-  def parseInput(separators: Set[String], terminators: Set[String], input: Reader[Char],

-                 justification: TextJustificationType.Type,

-                 padChar: String,

-                 isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    val hasDelim: Boolean = separators.size > 0 || terminators.size > 0

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)

-    val padCharRegex = generateCharacterRegex(padChar);

-

-    val pFieldAndDelim = justification match {

-      case TextJustificationType.None => generateInputNoPadParser(pSeps, pTerms,

-        delimsRegex, hasDelim, isMissingDelimAllowed)

-      case _ => generateInputWithPadParser(pSeps, pTerms, delimsRegex, padCharRegex,

-        justification, hasDelim, isMissingDelimAllowed)

-    }

-

-    val result = parseInputDefaultContent(pFieldAndDelim, pSeps, pTerms, input, justification)

-    result

-  }

-

-  private def parseInput_WithPad(separators: Set[String], terminators: Set[String], input: Reader[Char],

-                                 justification: TextJustificationType.Type,

-                                 padChar: String,

-                                 isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    val hasDelim: Boolean = separators.size > 0 || terminators.size > 0

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)

-    val padCharRegex = generateCharacterRegex(padChar);

-

-    val pFieldAndDelim = this.generateInputWithPadParser(pSeps, pTerms, delimsRegex, padCharRegex,

-      justification, hasDelim, isMissingDelimAllowed)

-

-    val result = parseInputDefaultContent(pFieldAndDelim, pSeps, pTerms, input, justification)

-    result

-  }

-

-  private def parseInput_NoPad(separators: Set[String], terminators: Set[String], input: Reader[Char],

-                               isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-    // TODO: Move regular expressions out to central class

-

-    val hasDelim: Boolean = separators.size > 0 || terminators.size > 0

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)

-

-    val pFieldAndDelim = this.generateInputNoPadParser(pSeps, pTerms, delimsRegex, hasDelim, isMissingDelimAllowed)

-

-    val result = parseInputDefaultContent(pFieldAndDelim, pSeps, pTerms, input, TextJustificationType.None)

-    result

-  }

-

-  /**

-   * Parses input that can have escape blocks

-   */

-  def parseInputEscapeBlock(separators: Set[String], terminators: Set[String],

-                            input: Reader[Char], escapeBlockStart: String, escapeBlockEnd: String,

-                            escapeEscapeCharacter: String = "",

-                            justification: TextJustificationType.Type,

-                            padChar: String,

-                            isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0

-    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)

-    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)

-    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)

-    val padCharRegex = generateCharacterRegex(padChar);

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-

-    val pFieldAndDelim = {

-      justification match {

-        case TextJustificationType.None => this.generateEscapeBlockNoPadParser(pSeps, pTerms,

-          escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)

-        case _ => this.generateEscapeBlockWithPadParser(pSeps, pTerms,

-          escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)

-      }

-    }

-

-    val result1 = parseInputEscapeBlockContent(pFieldAndDelim, pSeps, pTerms, input, justification)

-

-    // If failed, try regular parse.

-    val result = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapesBlocks(field, escapeEscapeRegex, escapeBlockEndRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),

-        //          s.delimiterLoc)

-

-      }

-      case f: DelimParseFailure => parseInput(separators, terminators, input, justification, padChar, isMissingDelimAllowed)

-    }

-    result

-  }

-

-  private def parseInputEscapeBlock_WithPad(separators: Set[String], terminators: Set[String],

-                                            input: Reader[Char], escapeBlockStart: String, escapeBlockEnd: String,

-                                            escapeEscapeCharacter: String = "",

-                                            justification: TextJustificationType.Type,

-                                            padChar: String,

-                                            isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    Assert.invariant(escapeBlockStart.length() != 0 && escapeBlockEnd.length() != 0 && padChar.length() != 0)

-

-    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0

-    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)

-    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)

-    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)

-    val padCharRegex = generateCharacterRegex(padChar);

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    //val delimsRegex = this.generateDelimsRegex(sepsRegex, termsRegex)

-

-    val pFieldAndDelim = generateEscapeBlockWithPadParser(pSeps, pTerms, escapeBlockStartRegex, escapeBlockEndRegex,

-      escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)

-

-    val result1 = parseInputEscapeBlockContent(pFieldAndDelim, pSeps, pTerms, input, justification)

-

-    // If failed, try regular parse.

-    val result = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapesBlocks(field, escapeEscapeRegex, escapeBlockEndRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),

-        //          s.delimiterLoc)

-

-      }

-      case f: DelimParseFailure => parseInput(separators, terminators, input, justification, padChar, isMissingDelimAllowed)

-    }

-    result

-  }

-

-  private def parseInputEscapeBlock_NoPad(separators: Set[String], terminators: Set[String],

-                                          input: Reader[Char], escapeBlockStart: String, escapeBlockEnd: String,

-                                          escapeEscapeCharacter: String = "",

-                                          isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-    //TODO: Move regular expressions out into central class

-    //if (escapeBlockStart.length() == 0 || escapeBlockEnd.length() == 0) { return failedResult }

-    Assert.invariant(escapeBlockStart.length() != 0 && escapeBlockEnd.length() != 0)

-

-    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0

-    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)

-    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)

-    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    //val delimsRegex = this.generateDelimsRegex(sepsRegex, termsRegex)

-

-    val pFieldAndDelim = generateEscapeBlockNoPadParser(pSeps, pTerms, escapeBlockStartRegex, escapeBlockEndRegex,

-      escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)

-

-    val result1 = parseInputEscapeBlockContent(pFieldAndDelim, pSeps, pTerms, input, TextJustificationType.None)

-

-    val result = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get // we move past this, though the resulting string will have escapes dropped.

-        val newField = removeEscapesBlocks(field, escapeEscapeRegex, escapeBlockEndRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-      }

-      // If failed, try regular parse.

-      case _: DelimParseFailure => parseInput(separators, terminators, input, TextJustificationType.None, "", isMissingDelimAllowed)

-    }

-    result

-  }

-

-  /**

-   * Parses input that can have character escape schemes

-   */

-  def parseInputEscapeCharacter(separators: Set[String], terminators: Set[String],

-                                input: Reader[Char], escapeCharacter: String, escapeEscapeCharacter: String = "",

-                                justification: TextJustificationType.Type,

-                                padChar: String,

-                                isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    Assert.invariant(escapeCharacter.length() != 0)

-

-    val hasEscEsc = escapeEscapeCharacter.length() > 0

-    val hasDelim = separators.size > 0 || terminators.size > 0

-    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)

-    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)

-    val padCharacterRegex = this.generateCharacterRegex(padChar)

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)

-

-    val pFieldAndDelim = {

-      if (escapeEscapeCharacter.equals(escapeCharacter)) {

-        justification match {

-          case TextJustificationType.None => this.generateEscapeCharacterSameNoPadParser(pSeps, pTerms,

-            escapeCharacterRegex, delimsRegex, hasDelim, isMissingDelimAllowed)

-          case _ => this.generateEscapeCharacterSameWithPadParser(pSeps, pTerms,

-            escapeCharacterRegex, delimsRegex, justification, padCharacterRegex, hasDelim, isMissingDelimAllowed)

-        }

-      } else {

-        justification match {

-          case TextJustificationType.None => this.generateEscapeCharacterDiffNoPadParser(pSeps, pTerms,

-            escapeCharacterRegex, escapeEscapeCharacterRegex, delimsRegex, hasEscEsc, hasDelim, isMissingDelimAllowed)

-          case _ => this.generateEscapeCharacterDiffWithPadParser(pSeps, pTerms,

-            escapeCharacterRegex, escapeEscapeCharacterRegex, padCharacterRegex, delimsRegex,

-            justification, hasEscEsc, hasDelim, isMissingDelimAllowed)

-        }

-      }

-    }

-    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, justification)

-    val result = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapeCharacters(field, escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),

-        //          s.delimiterLoc)

-      }

-      case f: DelimParseFailure => f

-    }

-    result

-  }

-

-  /*

-   * Parses input for escapeSchemeKind='character' when the escape and escapeEscape

-   * characters are not the same.

-   */

-  private def parseInputEscapeCharacter_DiffWithPad(separators: Set[String], terminators: Set[String],

-                                                    input: Reader[Char], escapeCharacter: String, escapeEscapeCharacter: String = "",

-                                                    justification: TextJustificationType.Type,

-                                                    padChar: String,

-                                                    isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    Assert.invariant(escapeCharacter.length() != 0 && padChar.length() != 0)

-

-    val hasEscEsc = escapeEscapeCharacter.length() > 0

-    val hasDelim = separators.size > 0 || terminators.size > 0

-    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)

-    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)

-    val padCharacterRegex = this.generateCharacterRegex(padChar)

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)

-    val pFieldAndDelim = this.generateEscapeCharacterDiffWithPadParser(pSeps, pTerms,

-      escapeCharacterRegex, escapeEscapeCharacterRegex, padCharacterRegex, delimsRegex, justification, hasEscEsc, hasDelim, isMissingDelimAllowed)

-

-    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, justification)

-    val result = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapeCharacters(field, escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),

-        //          s.delimiterLoc)

-      }

-      case f: DelimParseFailure => f

-    }

-    result

-  }

-

-  /*

-   * Parses input for escapeSchemeKind='character' when the escape and escapeEscape

-   * characters are not the same.

-   */

-  private def parseInputEscapeCharacter_DiffNoPad(separators: Set[String], terminators: Set[String],

-                                                  input: Reader[Char], escapeCharacter: String, escapeEscapeCharacter: String = "",

-                                                  isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    Assert.invariant(escapeCharacter.length() > 0)

-

-    val hasEscEsc = escapeEscapeCharacter.length() > 0

-    val hasDelim = separators.size > 0 || terminators.size > 0

-

-    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)

-    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)

-    val pFieldAndDelim = this.generateEscapeCharacterDiffNoPadParser(pSeps, pTerms,

-      escapeCharacterRegex, escapeEscapeCharacterRegex, delimsRegex, hasEscEsc, hasDelim, isMissingDelimAllowed)

-

-    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, TextJustificationType.None)

-    val result = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapeCharacters(field, escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),

-        //          s.delimiterLoc)

-      }

-      case f: DelimParseFailure => f

-    }

-    result

-  }

-

-  /* 

-   * Parses the input using escapeSchemeKind='character' for when the

-   * escape and escapeEscape are the same.

-   */

-  private def parseInputEscapeCharacter_SameNoPad(separators: Set[String], terminators: Set[String],

-                                                  input: Reader[Char], escapeCharacter: String,

-                                                  isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    //if (escapeCharacter.length() == 0) { return failedResult }

-    Assert.invariant(escapeCharacter.length > 0)

-

-    val hasDelim = separators.size > 0 || terminators.size > 0

-    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)

-    val pFieldAndDelim = this.generateEscapeCharacterSameNoPadParser(pSeps, pTerms,

-      escapeCharacterRegex, delimsRegex, hasDelim, isMissingDelimAllowed)

-

-    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, TextJustificationType.None)

-    val result = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapeCharacters(field, escapeCharacterRegex, escapeCharacterRegex, delimsRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),

-        //          s.delimiterLoc)

-      }

-      case f: DelimParseFailure => f

-    }

-    result

-  }

-

-  /* 

-   * Parses the input using escapeSchemeKind='character' for when the

-   * escape and escapeEscape are the same.

-   */

-  private def parseInputEscapeCharacter_SameWithPad(separators: Set[String], terminators: Set[String],

-                                                    input: Reader[Char], escapeCharacter: String,

-                                                    justification: TextJustificationType.Type,

-                                                    padChar: String,

-                                                    isMissingDelimAllowed: Boolean = true): DelimParseResult = {

-

-    // if (escapeCharacter.length() == 0 || padChar.length() == 0) { return failedResult }

-    Assert.invariant(escapeCharacter.length() > 0 && padChar.length() > 0)

-

-    val hasDelim = separators.size > 0 || terminators.size > 0

-    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)

-    val padCharacterRegex = this.generateCharacterRegex(padChar)

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)

-    val pFieldAndDelim = this.generateEscapeCharacterSameWithPadParser(pSeps, pTerms,

-      escapeCharacterRegex, delimsRegex, justification, padChar, hasDelim, isMissingDelimAllowed)

-

-    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, justification)

-    val result = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapeCharacters(field, escapeCharacterRegex, escapeCharacterRegex, delimsRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),

-        //          s.delimiterLoc)

-      }

-      case f: DelimParseFailure => f

-    }

-    result

-  }

-

-  /**

-   * Assumes 'input' has had its delimiter picked off end already if it existed.

-   */

-  def removeEscapeCharacters(input: String, eses: String, es: String, delimRegex: String): String = {

-    if (eses.equals(es)) {

-      return removeEscapeCharactersSame(input, es)

-    } else {

-      return removeEscapeCharactersDiff(input, eses, es)

-    }

-  }

-

-  private def removeEscapeCharactersSame(input: String, es: String): String = {

-    // used to cleanup escape characters 

-    val ERSplit = this.generateRemoveEscapeCharactersSameRegex(es)

-    def removeActiveEscapes(str: String): String = {

-      val res = str match {

-        case ERSplit(before, theEsc, delim, after) => {

-          val rest = removeActiveEscapes(after)

-          before + delim + rest

-        }

-        case ERSplit(before, delim, after) => {

-          val rest = removeActiveEscapes(after)

-          before + delim + rest

-        }

-        case _ => str

-      }

-      res

-    }

-    removeActiveEscapes(input)

-  }

-

-  private def removeEscapeCharactersDiff(input: String, eses: String, es: String): String = {

-    // TODO: Move regular expressions out into central class

-    if (eses.length() > 0) {

-      val removeUnescapedEscapes = this.removeUnescapedEscapesRegex(eses, es) //rRemoveUnescapedEscapes.format(eses, es)

-      val removeEscapeEscapesThatEscape = this.removeEscapeEscapesThatEscapeRegex(eses, es) //rRemoveEscapeEscapesThatEscape.format(eses, es)

-      val r1 = input.replaceAll(removeUnescapedEscapes, "")

-      val r2 = r1.replaceAll(removeEscapeEscapesThatEscape, "")

-      return r2

-    }

-    val rRemoveEscape = this.removeEscapeRegex(es)

-    val r1 = input.replaceAll(rRemoveEscape, "")

-    r1

-  }

-

-  /**

-   * Assumes that valid escape block start and end were already removed.

-   */

-  def removeEscapesBlocks(input: String, esesRegex: String, endBlockRegex: String): String = {

-    val removeEscapes = this.removeEscapesBlocksRegex(esesRegex, endBlockRegex)

-    input.replaceAll(removeEscapes, "")

-  }

-

-}

+ */
+
+import scala.annotation.migration
+import scala.collection.mutable.Queue
+import scala.util.parsing.combinator.RegexParsers
+import scala.util.parsing.input.Reader
+import edu.illinois.ncsa.daffodil.processors.DelimiterType._
+import edu.illinois.ncsa.daffodil.processors.DelimiterLocation._
+import scala.util.matching.Regex
+import java.nio.charset.Charset
+import java.util.regex.Pattern
+import edu.illinois.ncsa.daffodil.util.Logging
+import edu.illinois.ncsa.daffodil.util.LogLevel
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.dsom.AnnotatedSchemaComponent
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import edu.illinois.ncsa.daffodil.util.Info
+
+class DelimParser(stringBitLengthFunction: String => Int) extends DFDLDelimParserCommon(stringBitLengthFunction) {
+
+  def removePadding(input: String, justification: TextJustificationType.Type, padChar: String): String = {
+    if ((padChar.length() == 0)) { return input }
+    val field = this.generateRemovePaddingParser(justification, padChar)
+    val result = field match {
+      case None => input
+      case Some(f) => {
+        val res = this.parse(this.log(f)({
+          justification match {
+            case TextJustificationType.Left => "DelimParser.removePadding.leftJustified"
+            case TextJustificationType.Right => "DelimParser.removePadding.rightJustified"
+            case TextJustificationType.Center => "DelimParser.removePadding.centerJustified"
+            case TextJustificationType.None => Assert.invariantFailed("should not be none if we're trimming.")
+          }
+        }), input)
+        res.getOrElse(input)
+      }
+    }
+    result
+  }
+
+  def parseInputPatterned(pattern: String, input: Reader[Char]): DelimParseResult = {
+    val entry = this.generateInputPatternedParser(pattern)
+
+    // FOR DEBUGGING might want this logging version
+    val res = this.parse(this.log(entry)("DelimParser.parseInputPatterned"), input)
+    //val res = this.parse(entry, input)
+
+    res match {
+      case s @ Success(_, _) => DelimParseSuccessFactory(s, "", DelimiterType.NotDelimited, None, DelimiterLocation.Local)
+      case f: NoSuccess => DelimParseFailure(f.msg, f.next)
+    }
+  }
+
+  def parseInputNCharacters(nChars: Long, input: Reader[Char],
+                            justification: TextJustificationType.Type,
+                            padChar: String): DelimParseResult = {
+
+    val entry = this.generateInputNCharactersParser(nChars)
+
+    // For debug can use this logging parser instead.
+    val res = this.parse(this.log(entry)("DelimParser.parseInputNCharacters"), input)
+    //val res = this.parse(entry, input)
+
+    res match {
+      case s @ Success(field, next) => {
+        val fieldNoPadding = removePadding(field, justification, padChar)
+        DelimParseSuccessFactory(Success(fieldNoPadding, next), "", DelimiterType.NotDelimited, Some(field), DelimiterLocation.Local)
+      }
+      case f: NoSuccess => DelimParseFailure(f.msg, f.next)
+    }
+  }
+
+  //  private def parseInputEscapeCharContent(fieldParser: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],
+  //    input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {
+  //    parseInputDefaultContent(fieldParser, seps, terms, input, justification)
+  //  }
+
+  /**
+   * localDelims - delimiters local to the component in question
+   * remoteDelims - delimiters of an enclosing container of this component
+   *
+   * Assumes that remoteDelims does not contain any String found in localDelims
+   *
+   * The call to buildDelims sorts the delimiters by length or possible length.
+   */
+  def parseInputDelimiter(localDelims: Set[String], remoteDelims: Set[String],
+                          input: Reader[Char]): DelimParseResult = {
+
+    val (localDelimsParser, localDelimsRegex) = generateDelimiter(localDelims)
+    val (remoteDelimsParser, remoteDelimsRegex) = generateDelimiter(remoteDelims)
+    val entry = generateInputDelimiterParser(localDelimsParser, remoteDelimsParser)
+
+    val res = this.parse(this.log(entry)("DelimParser.parseInputDelimiter.allDelims"), input)
+
+    // TODO: This seems pretty inefficient. We're redoing a match in order to know 
+    // whether it was local or remote?? 
+    val result = res match {
+      case s @ Success(delimiterResult, next) => {
+        // We have a result but was it a remote or local match?
+        // We need the regex to match exactly the whole delimiterResult
+        // Here localDelimsRegex should have already been sorted by the buildDelims call
+        // we simply need to tell the regex that it has to match the full delimiterResult String.
+        // If it doesn't match, then that means the match had to be a remote delimiter.
+        val newLocalDelimsRegex = "(?s)^(" + combineDelimitersRegex(localDelimsRegex, Array.empty[String]) + ")$"
+        val newLocalDelimsParser: Parser[String] = newLocalDelimsRegex.r
+
+        val subResult = this.parseAll(this.log(newLocalDelimsParser)("DelimParser.parseInputDelimiter.isLocal"), delimiterResult)
+        val delimiterLocation = if (subResult.isEmpty) DelimiterLocation.Remote else DelimiterLocation.Local
+        //
+        // TODO: ?? Is None the right thing to pass here?? If we pass none, then it is 
+        // going to determine the length based on the delimiterResult. Does that include
+        // everything it needs to include?
+        //
+        DelimParseSuccessFactory(s, delimiterResult, DelimiterType.NotDelimited, None, // Is None right?
+          delimiterLocation)
+      }
+      case NoSuccess(msg, next) => DelimParseFailure(msg, next)
+    }
+    result
+  }
+
+  /**
+   * Parses text input without escape schemes
+   */
+  def parseInput(separators: Set[String], terminators: Set[String], input: Reader[Char],
+                 justification: TextJustificationType.Type,
+                 padChar: String,
+                 isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    val hasDelim: Boolean = separators.size > 0 || terminators.size > 0
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)
+    val padCharRegex = generateCharacterRegex(padChar);
+
+    val pFieldAndDelim = justification match {
+      case TextJustificationType.None => generateInputNoPadParser(pSeps, pTerms,
+        delimsRegex, hasDelim, isMissingDelimAllowed)
+      case _ => generateInputWithPadParser(pSeps, pTerms, delimsRegex, padCharRegex,
+        justification, hasDelim, isMissingDelimAllowed)
+    }
+
+    val result = parseInputDefaultContent(pFieldAndDelim, pSeps, pTerms, input, justification)
+    result
+  }
+
+  private def parseInput_WithPad(separators: Set[String], terminators: Set[String], input: Reader[Char],
+                                 justification: TextJustificationType.Type,
+                                 padChar: String,
+                                 isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    val hasDelim: Boolean = separators.size > 0 || terminators.size > 0
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)
+    val padCharRegex = generateCharacterRegex(padChar);
+
+    val pFieldAndDelim = this.generateInputWithPadParser(pSeps, pTerms, delimsRegex, padCharRegex,
+      justification, hasDelim, isMissingDelimAllowed)
+
+    val result = parseInputDefaultContent(pFieldAndDelim, pSeps, pTerms, input, justification)
+    result
+  }
+
+  private def parseInput_NoPad(separators: Set[String], terminators: Set[String], input: Reader[Char],
+                               isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+    // TODO: Move regular expressions out to central class
+
+    val hasDelim: Boolean = separators.size > 0 || terminators.size > 0
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)
+
+    val pFieldAndDelim = this.generateInputNoPadParser(pSeps, pTerms, delimsRegex, hasDelim, isMissingDelimAllowed)
+
+    val result = parseInputDefaultContent(pFieldAndDelim, pSeps, pTerms, input, TextJustificationType.None)
+    result
+  }
+
+  /**
+   * Parses input that can have escape blocks
+   */
+  def parseInputEscapeBlock(separators: Set[String], terminators: Set[String],
+                            input: Reader[Char], escapeBlockStart: String, escapeBlockEnd: String,
+                            escapeEscapeCharacter: String = "",
+                            justification: TextJustificationType.Type,
+                            padChar: String,
+                            isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0
+    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)
+    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)
+    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)
+    val padCharRegex = generateCharacterRegex(padChar);
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+
+    val pFieldAndDelim = {
+      justification match {
+        case TextJustificationType.None => this.generateEscapeBlockNoPadParser(pSeps, pTerms,
+          escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)
+        case _ => this.generateEscapeBlockWithPadParser(pSeps, pTerms,
+          escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)
+      }
+    }
+
+    val result1 = parseInputEscapeBlockContent(pFieldAndDelim, pSeps, pTerms, input, justification)
+
+    // If failed, try regular parse.
+    val result = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapesBlocks(field, escapeEscapeRegex, escapeBlockEndRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),
+        //          s.delimiterLoc)
+
+      }
+      case f: DelimParseFailure => parseInput(separators, terminators, input, justification, padChar, isMissingDelimAllowed)
+    }
+    result
+  }
+
+  private def parseInputEscapeBlock_WithPad(separators: Set[String], terminators: Set[String],
+                                            input: Reader[Char], escapeBlockStart: String, escapeBlockEnd: String,
+                                            escapeEscapeCharacter: String = "",
+                                            justification: TextJustificationType.Type,
+                                            padChar: String,
+                                            isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    Assert.invariant(escapeBlockStart.length() != 0 && escapeBlockEnd.length() != 0 && padChar.length() != 0)
+
+    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0
+    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)
+    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)
+    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)
+    val padCharRegex = generateCharacterRegex(padChar);
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    //val delimsRegex = this.generateDelimsRegex(sepsRegex, termsRegex)
+
+    val pFieldAndDelim = generateEscapeBlockWithPadParser(pSeps, pTerms, escapeBlockStartRegex, escapeBlockEndRegex,
+      escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)
+
+    val result1 = parseInputEscapeBlockContent(pFieldAndDelim, pSeps, pTerms, input, justification)
+
+    // If failed, try regular parse.
+    val result = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapesBlocks(field, escapeEscapeRegex, escapeBlockEndRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),
+        //          s.delimiterLoc)
+
+      }
+      case f: DelimParseFailure => parseInput(separators, terminators, input, justification, padChar, isMissingDelimAllowed)
+    }
+    result
+  }
+
+  private def parseInputEscapeBlock_NoPad(separators: Set[String], terminators: Set[String],
+                                          input: Reader[Char], escapeBlockStart: String, escapeBlockEnd: String,
+                                          escapeEscapeCharacter: String = "",
+                                          isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+    //TODO: Move regular expressions out into central class
+    //if (escapeBlockStart.length() == 0 || escapeBlockEnd.length() == 0) { return failedResult }
+    Assert.invariant(escapeBlockStart.length() != 0 && escapeBlockEnd.length() != 0)
+
+    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0
+    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)
+    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)
+    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    //val delimsRegex = this.generateDelimsRegex(sepsRegex, termsRegex)
+
+    val pFieldAndDelim = generateEscapeBlockNoPadParser(pSeps, pTerms, escapeBlockStartRegex, escapeBlockEndRegex,
+      escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)
+
+    val result1 = parseInputEscapeBlockContent(pFieldAndDelim, pSeps, pTerms, input, TextJustificationType.None)
+
+    val result = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get // we move past this, though the resulting string will have escapes dropped.
+        val newField = removeEscapesBlocks(field, escapeEscapeRegex, escapeBlockEndRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+      }
+      // If failed, try regular parse.
+      case _: DelimParseFailure => parseInput(separators, terminators, input, TextJustificationType.None, "", isMissingDelimAllowed)
+    }
+    result
+  }
+
+  /**
+   * Parses input that can have character escape schemes
+   */
+  def parseInputEscapeCharacter(separators: Set[String], terminators: Set[String],
+                                input: Reader[Char], escapeCharacter: String, escapeEscapeCharacter: String = "",
+                                justification: TextJustificationType.Type,
+                                padChar: String,
+                                isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    Assert.invariant(escapeCharacter.length() != 0)
+
+    val hasEscEsc = escapeEscapeCharacter.length() > 0
+    val hasDelim = separators.size > 0 || terminators.size > 0
+    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)
+    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)
+    val padCharacterRegex = this.generateCharacterRegex(padChar)
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)
+
+    val pFieldAndDelim = {
+      if (escapeEscapeCharacter.equals(escapeCharacter)) {
+        justification match {
+          case TextJustificationType.None => this.generateEscapeCharacterSameNoPadParser(pSeps, pTerms,
+            escapeCharacterRegex, delimsRegex, hasDelim, isMissingDelimAllowed)
+          case _ => this.generateEscapeCharacterSameWithPadParser(pSeps, pTerms,
+            escapeCharacterRegex, delimsRegex, justification, padCharacterRegex, hasDelim, isMissingDelimAllowed)
+        }
+      } else {
+        justification match {
+          case TextJustificationType.None => this.generateEscapeCharacterDiffNoPadParser(pSeps, pTerms,
+            escapeCharacterRegex, escapeEscapeCharacterRegex, delimsRegex, hasEscEsc, hasDelim, isMissingDelimAllowed)
+          case _ => this.generateEscapeCharacterDiffWithPadParser(pSeps, pTerms,
+            escapeCharacterRegex, escapeEscapeCharacterRegex, padCharacterRegex, delimsRegex,
+            justification, hasEscEsc, hasDelim, isMissingDelimAllowed)
+        }
+      }
+    }
+    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, justification)
+    val result = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapeCharacters(field, escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),
+        //          s.delimiterLoc)
+      }
+      case f: DelimParseFailure => f
+    }
+    result
+  }
+
+  /*
+   * Parses input for escapeSchemeKind='character' when the escape and escapeEscape
+   * characters are not the same.
+   */
+  private def parseInputEscapeCharacter_DiffWithPad(separators: Set[String], terminators: Set[String],
+                                                    input: Reader[Char], escapeCharacter: String, escapeEscapeCharacter: String = "",
+                                                    justification: TextJustificationType.Type,
+                                                    padChar: String,
+                                                    isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    Assert.invariant(escapeCharacter.length() != 0 && padChar.length() != 0)
+
+    val hasEscEsc = escapeEscapeCharacter.length() > 0
+    val hasDelim = separators.size > 0 || terminators.size > 0
+    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)
+    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)
+    val padCharacterRegex = this.generateCharacterRegex(padChar)
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)
+    val pFieldAndDelim = this.generateEscapeCharacterDiffWithPadParser(pSeps, pTerms,
+      escapeCharacterRegex, escapeEscapeCharacterRegex, padCharacterRegex, delimsRegex, justification, hasEscEsc, hasDelim, isMissingDelimAllowed)
+
+    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, justification)
+    val result = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapeCharacters(field, escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),
+        //          s.delimiterLoc)
+      }
+      case f: DelimParseFailure => f
+    }
+    result
+  }
+
+  /*
+   * Parses input for escapeSchemeKind='character' when the escape and escapeEscape
+   * characters are not the same.
+   */
+  private def parseInputEscapeCharacter_DiffNoPad(separators: Set[String], terminators: Set[String],
+                                                  input: Reader[Char], escapeCharacter: String, escapeEscapeCharacter: String = "",
+                                                  isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    Assert.invariant(escapeCharacter.length() > 0)
+
+    val hasEscEsc = escapeEscapeCharacter.length() > 0
+    val hasDelim = separators.size > 0 || terminators.size > 0
+
+    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)
+    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)
+    val pFieldAndDelim = this.generateEscapeCharacterDiffNoPadParser(pSeps, pTerms,
+      escapeCharacterRegex, escapeEscapeCharacterRegex, delimsRegex, hasEscEsc, hasDelim, isMissingDelimAllowed)
+
+    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, TextJustificationType.None)
+    val result = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapeCharacters(field, escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),
+        //          s.delimiterLoc)
+      }
+      case f: DelimParseFailure => f
+    }
+    result
+  }
+
+  /* 
+   * Parses the input using escapeSchemeKind='character' for when the
+   * escape and escapeEscape are the same.
+   */
+  private def parseInputEscapeCharacter_SameNoPad(separators: Set[String], terminators: Set[String],
+                                                  input: Reader[Char], escapeCharacter: String,
+                                                  isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    //if (escapeCharacter.length() == 0) { return failedResult }
+    Assert.invariant(escapeCharacter.length > 0)
+
+    val hasDelim = separators.size > 0 || terminators.size > 0
+    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)
+    val pFieldAndDelim = this.generateEscapeCharacterSameNoPadParser(pSeps, pTerms,
+      escapeCharacterRegex, delimsRegex, hasDelim, isMissingDelimAllowed)
+
+    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, TextJustificationType.None)
+    val result = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapeCharacters(field, escapeCharacterRegex, escapeCharacterRegex, delimsRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),
+        //          s.delimiterLoc)
+      }
+      case f: DelimParseFailure => f
+    }
+    result
+  }
+
+  /* 
+   * Parses the input using escapeSchemeKind='character' for when the
+   * escape and escapeEscape are the same.
+   */
+  private def parseInputEscapeCharacter_SameWithPad(separators: Set[String], terminators: Set[String],
+                                                    input: Reader[Char], escapeCharacter: String,
+                                                    justification: TextJustificationType.Type,
+                                                    padChar: String,
+                                                    isMissingDelimAllowed: Boolean = true): DelimParseResult = {
+
+    // if (escapeCharacter.length() == 0 || padChar.length() == 0) { return failedResult }
+    Assert.invariant(escapeCharacter.length() > 0 && padChar.length() > 0)
+
+    val hasDelim = separators.size > 0 || terminators.size > 0
+    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)
+    val padCharacterRegex = this.generateCharacterRegex(padChar)
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = this.combineDelimitersRegex(sepsRegex, termsRegex)
+    val pFieldAndDelim = this.generateEscapeCharacterSameWithPadParser(pSeps, pTerms,
+      escapeCharacterRegex, delimsRegex, justification, padChar, hasDelim, isMissingDelimAllowed)
+
+    val result1 = parseInputEscapeCharContent(pFieldAndDelim, pSeps, pTerms, input, justification)
+    val result = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapeCharacters(field, escapeCharacterRegex, escapeCharacterRegex, delimsRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+        //        DelimParseSuccessFactory(Success(newField, s.next), s.delimiter, s.delimiterType, Some(field),
+        //          s.delimiterLoc)
+      }
+      case f: DelimParseFailure => f
+    }
+    result
+  }
+
+  /**
+   * Assumes 'input' has had its delimiter picked off end already if it existed.
+   */
+  def removeEscapeCharacters(input: String, eses: String, es: String, delimRegex: String): String = {
+    if (eses.equals(es)) {
+      return removeEscapeCharactersSame(input, es)
+    } else {
+      return removeEscapeCharactersDiff(input, eses, es)
+    }
+  }
+
+  private def removeEscapeCharactersSame(input: String, es: String): String = {
+    // used to cleanup escape characters 
+    val ERSplit = this.generateRemoveEscapeCharactersSameRegex(es)
+    def removeActiveEscapes(str: String): String = {
+      val res = str match {
+        case ERSplit(before, theEsc, delim, after) => {
+          val rest = removeActiveEscapes(after)
+          before + delim + rest
+        }
+        case ERSplit(before, delim, after) => {
+          val rest = removeActiveEscapes(after)
+          before + delim + rest
+        }
+        case _ => str
+      }
+      res
+    }
+    removeActiveEscapes(input)
+  }
+
+  private def removeEscapeCharactersDiff(input: String, eses: String, es: String): String = {
+    // TODO: Move regular expressions out into central class
+    if (eses.length() > 0) {
+      val removeUnescapedEscapes = this.removeUnescapedEscapesRegex(eses, es) //rRemoveUnescapedEscapes.format(eses, es)
+      val removeEscapeEscapesThatEscape = this.removeEscapeEscapesThatEscapeRegex(eses, es) //rRemoveEscapeEscapesThatEscape.format(eses, es)
+      val r1 = input.replaceAll(removeUnescapedEscapes, "")
+      val r2 = r1.replaceAll(removeEscapeEscapesThatEscape, "")
+      return r2
+    }
+    val rRemoveEscape = this.removeEscapeRegex(es)
+    val r1 = input.replaceAll(rRemoveEscape, "")
+    r1
+  }
+
+  /**
+   * Assumes that valid escape block start and end were already removed.
+   */
+  def removeEscapesBlocks(input: String, esesRegex: String, endBlockRegex: String): String = {
+    val removeEscapes = this.removeEscapesBlocksRegex(esesRegex, endBlockRegex)
+    input.replaceAll(removeEscapes, "")
+  }
+
+}
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParserCommon.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParserCommon.scala
index 5925edd..494fb93 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParserCommon.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParserCommon.scala
@@ -1,1435 +1,1435 @@
-package edu.illinois.ncsa.daffodil.processors

-

-/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.

- *

- * Developed by: Tresys Technology, LLC

- *               http://www.tresys.com

- * 

- * Permission is hereby granted, free of charge, to any person obtaining a copy of

- * this software and associated documentation files (the "Software"), to deal with

- * the Software without restriction, including without limitation the rights to

- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies

- * of the Software, and to permit persons to whom the Software is furnished to do

- * so, subject to the following conditions:

- * 

- *  1. Redistributions of source code must retain the above copyright notice,

- *     this list of conditions and the following disclaimers.

- * 

- *  2. Redistributions in binary form must reproduce the above copyright

- *     notice, this list of conditions and the following disclaimers in the

- *     documentation and/or other materials provided with the distribution.

- * 

- *  3. Neither the names of Tresys Technology, nor the names of its contributors

- *     may be used to endorse or promote products derived from this Software

- *     without specific prior written permission.

- * 

- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

- * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE

- * SOFTWARE.

- */

-

-import scala.util.parsing.combinator.RegexParsers

-import edu.illinois.ncsa.daffodil.util.Logging

-import scala.util.parsing.input.Reader

-import edu.illinois.ncsa.daffodil.util.Logging

-import edu.illinois.ncsa.daffodil.util.LogLevel

-import edu.illinois.ncsa.daffodil.util._

-import scala.collection.mutable.Queue

-import java.util.regex.Pattern

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import edu.illinois.ncsa.daffodil.processors.DelimiterType._

-import edu.illinois.ncsa.daffodil.processors.DelimiterLocation._

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-

-trait DebugRegexParsers extends RegexParsers with Logging {

-  /**

-   * Used to trace parsing

-   */

-  implicit def toLogged(name: String) = new {

-    def !!![T](p: Parser[T]) = log(p)(name)

-  }

-

-  /**

-   * A helper method that turns a `Parser` into one that will

-   *  print debugging information to stdout before and after

-   *  being applied.

-   */

-  override def log[T](p: => Parser[T])(name: String): Parser[T] = Parser { in =>

-    log(LogLevel.DelimDebug, "trying %s at %s", name, in)

-    val r = p(in)

-    log(LogLevel.DelimDebug, "end %s --> %s", name, r)

-    r

-  }

-

-}

-

-object TextJustificationType extends Enum {

-  sealed abstract trait Type extends EnumValueType

-  case object None extends Type

-  case object Left extends Type

-  case object Right extends Type

-  case object Center extends Type

-}

-

-sealed abstract class DelimParseResult(nextArg: Reader[Char]) {

-  def isSuccess: Boolean

-  def next = nextReader.asInstanceOf[DFDLCharReader]

-  def nextReader = nextArg

-}

-

-case class DelimParseSuccess(val delimiter: String,

-  val delimiterType: DelimiterType.Type,

-  val delimiterLoc: DelimiterLocation.Type,

-  val numBits: Int,

-  fieldArg: String,

-  nextArg: Reader[Char],

-  val numCharsRead: Int)

-  extends DelimParseResult(nextArg) {

-  def isSuccess = true

-  def field = fieldArg

-  def get = field

-}

-

-case class DelimParseFailure(msgArg: String, nextArg: Reader[Char])

-  extends DelimParseResult(nextArg) {

-  def isSuccess = false

-  def msg = msgArg

-}

-

-class DFDLDelimParserCommon(stringBitLengthFunction: String => Int) extends RegexParsers with DebugRegexParsers {

-  /**

-   * Thisobject has to be nested because it has as an argument type Success[String]

-   * and that type is only availble to things that implement the scala...Parsers trait.

-   *

-   * This is why you don't want to ball up all your stuff into a trait, you make reuse

-   * by derivation work, but you make reuse by encapsulation very difficult.

-   */

-  object DelimParseSuccessFactory {

-    /**

-     * If content is supplied then it is used to determine the field length.

-     * If None then the extracted field value itself is used.

-     */

-    def apply(res: Success[String], delimiter: String, delimiterType: DelimiterType.Type, contentOpt: Option[String],

-      dLoc: DelimiterLocation.Type) = {

-

-      val Success(fieldResult, next) = res

-      val content = contentOpt.getOrElse(res.get)

-      val charLength = content.length

-      val fieldResultBits = stringBitLengthFunction(content)

-      val result = new DelimParseSuccess(delimiter, delimiterType,

-        dLoc, fieldResultBits, fieldResult, next, charLength)

-      result

-    }

-  }

-

-  override val skipWhitespace = false

-

-  /**

-   * Need a parser that will always fail.

-   * Essentially a parser to reflect the ability to pass in an empty Separator

-   * or empty Terminator Set.

-   */

-  def parserAlwaysFail[T](expected: String)(name: String) = new Parser[T] {

-    def apply(in: Input): ParseResult[T] = {

-      Failure(name + " expected to fail.", in)

-    }

-  }

-

-  /**

-   * Constructs an Array of Parser[String] which holds the Parser representations

-   * of the delimList.

-   *

-   * Constructs an Array of String which holds the Regex representations of the

-   * delimList.

-   */

-  def buildDelims(delimList: Set[String]): (Array[Parser[String]], Array[String]) = {

-    var delimsParser: Queue[Parser[String]] = Queue.empty

-    var delimsRegex: Queue[String] = Queue.empty

-

-    // We probably always want delims ordered:

-    // Multi-char delims containing WSP+/*, WSP+, WSP*, multi-char delims, WSP, single-char delims

-

-    sortDelims(delimList).toList.foreach(str => {

-      val d = new Delimiter()

-      d(str)

-      delimsParser.enqueue(d.delimRegExParseDelim.r) // The regex representing the actual delimiter

-      delimsRegex.enqueue(d.delimRegExParseDelim) // The regex representing the actual delimiter

-    })

-    (delimsParser.toArray, delimsRegex.toArray)

-  }

-

-  /**

-   * Receives a Set of delimiters.  Returns a Set of (DelimRegexStr, OrigDelimStr)

-   */

-  def getDelimsRegex(delimList: Set[String]): Set[(String, String)] = {

-    var delims: Queue[(String, String)] = Queue.empty

-

-    delimList.foreach(str => {

-      val d = new Delimiter()

-      d(str)

-      delims.enqueue((d.delimRegExParseDelim, str))

-    })

-    delims.toSet

-  }

-

-  def sortDelims(delimList: Set[String]): Seq[String] = {

-    val wspStarByItself = delimList.filter(s => s == "%WSP*;")

-    val wspPlusByItself = delimList.filter(s => s == "%WSP+;")

-

-    val filteredDelimList = (delimList -- (wspStarByItself union wspPlusByItself))

-

-    val multiCharUnboundedLength = filteredDelimList.filter(s => (s.contains("%WSP*;") || s.contains("%WSP+;")))

-    val multiChar = (filteredDelimList -- multiCharUnboundedLength).filter(s => s.length() > 1)

-    val singleChar = filteredDelimList -- (multiChar union multiCharUnboundedLength)

-

-    val sortedUnbounded = multiCharUnboundedLength.toArray[String]

-    val sortedMultiChar = multiChar.toArray[String]

-

-    scala.util.Sorting.quickSort(sortedUnbounded)

-    scala.util.Sorting.quickSort(sortedMultiChar)

-

-    val orderedResultSeq: Seq[String] = sortedUnbounded.reverse.toSeq ++ wspPlusByItself ++ wspStarByItself ++ sortedMultiChar.reverse.toSeq ++ singleChar

-    orderedResultSeq

-  }

-

-  /**

-   * Combines the delimiters into a single alternation

-   */

-  def combineDelimitersRegex(sepsRegex: Array[String], termsRegex: Array[String]): String = {

-    val sb = new StringBuilder()

-    sepsRegex.foreach(x => {

-      sb.append(x)

-      sb.append("|")

-    })

-    termsRegex.foreach(x => {

-      sb.append(x)

-      sb.append("|")

-    })

-    val delimRegex = sb.toString().replaceFirst("[\\|]$", "") // trimEnd("|")

-    //    val seps = sepsRegex.mkString("|")

-    //    val terms = termsRegex.mkString("|")

-    //    val delimRegex = (seps + terms) //.replaceFirst("[\\|]$", "")

-    delimRegex

-  }

-

-  /**

-   * This function performs the "longest match" alternation by recursively combining

-   * all of the Parsers present in pArray.

-   *

-   * Assumes pArray is at least of length 1.

-   *

-   * pArray: An array of delimiters where each delimiter is represented by a Parser

-   * idx: The index of the current delimiter/Parser, defaults to 0

-   */

-  def combineLongest[T](pArray: Array[Parser[T]], idx: Int = 0): Parser[T] = {

-    val len = pArray.length

-

-    if (len == 0) { return parserAlwaysFail("")("empty delimiter list") }

-

-    val p0 = pArray(idx)

-

-    if ((idx + 1) == len) { p0 }

-    else {

-      (p0) ||| (combineLongest(pArray, idx + 1))

-    }

-  }

-

-  def convertDFDLLiteralToRegex(dfdlLiteral: String): String = {

-    var sb: StringBuilder = new StringBuilder("(")

-

-    dfdlLiteral foreach {

-      char =>

-        {

-          char match {

-            case '[' => sb.append("\\[")

-            case '\\' => sb.append("\\\\")

-            case '^' => sb.append("\\^")

-            case '$' => sb.append("\\$")

-            case '.' => sb.append("\\.")

-            case '|' => sb.append("\\|")

-            case '?' => sb.append("\\?")

-            case '*' => sb.append("\\*")

-            case '+' => sb.append("\\+")

-            case '(' => sb.append("\\(")

-            case ')' => sb.append("\\)")

-            case '{' => sb.append("\\{")

-            case '}' => sb.append("\\}")

-            case x => sb.append(x)

-          }

-        }

-    }

-    sb.append(")")

-    val res = sb.toString()

-    if (res.equals("()")) "" // get rid of empty group in regular expressions

-    else res

-  }

-

-  def getDfdlLiteralRegex(dfdlLiteralList: Set[String]): String = {

-    val (_, regex) = this.buildDelims(dfdlLiteralList)

-    combineDelimitersRegex(regex, Array.empty[String])

-  }

-

-  // TODO: does this handle %ES; or do we have to have outside separate checks for that?

-  // There is a separate check right now in LiteralNilDelimitedOrEndOfData. 

-  def isFieldDfdlLiteral(field: String, dfdlLiteralList: Set[String]): Boolean = {

-    val dfdlLiteralRegex = getDfdlLiteralRegex(dfdlLiteralList)

-    val m = Pattern.compile(dfdlLiteralRegex).matcher(field)

-    m.find()

-    m.matches()

-  }

-

-  lazy val EOF: Parser[String] = """\z""".r

-

-  def generateRemovePaddingParser(justification: TextJustificationType.Type, padChar: String): Option[Parser[String]] = {

-    if (padChar.length() == 0) return None

-    //val rAnything = """(.*?)(?=%s*$)""".format(padChar)

-    val rAnything = """(.*?)(?=""" + padChar + """*$)"""

-    val anything: Parser[String] = "generateRemovePaddingParser.anything".!!!(rAnything.r)

-    val padCharRegex = this.convertDFDLLiteralToRegex(padChar)

-    //val rPadCharLeft = """^(%s*)""".format(padCharRegex)

-    val rPadCharLeft = """^(""" + padCharRegex + """*)"""

-    val pPadCharLeft: Parser[String] = "generateRemovePaddingParser.pPadCharLeft".!!!(rPadCharLeft.r)

-    //val rPadCharRight = """(%s*)$""".format(padCharRegex)

-    val rPadCharRight = """(""" + padCharRegex + """*)$"""

-    val pPadCharRight: Parser[String] = "generateRemovePaddingParser.pPadCharRight".!!!(rPadCharRight.r)

-    val fieldCenter: Parser[String] = "generateRemovePaddingParser.fieldCenter".!!!(pPadCharLeft ~! anything ~! pPadCharRight) ^^ { case (l ~ a ~ r) => a }

-    val fieldLeft: Parser[String] = "generateRemovePaddingParser.fieldLeft".!!!(anything ~! pPadCharRight) ^^ { case (a ~ r) => a }

-    val fieldRight: Parser[String] = "generateRemovePaddingParser.fieldRight".!!!(pPadCharLeft ~! anything) ^^ { case (l ~ a) => a }

-    val result: Option[Parser[String]] = justification match {

-      case TextJustificationType.None => None

-      case TextJustificationType.Left => Some(fieldLeft)

-      case TextJustificationType.Right => Some(fieldRight)

-      case TextJustificationType.Center => Some(fieldCenter)

-    }

-    result

-  }

-

-  def generateInputPatternedParser(pattern: String): Parser[String] = {

-    val thePattern: Parser[String] = "generateInputPatternedParser.thePattern".!!!(("(?s)" + pattern).r)

-    //val entry = thePattern <~ opt(EOF)

-    //    val e0 = thePattern

-    //    val e1 = thePattern ~! EOF ^^ { case(p ~ e) => p}

-    //    val entry = e0 ||| e1

-    val entry = "generateInputPatternedParser.entry".!!!(thePattern ~! opt(EOF)) ^^ {

-      case (p ~ _) => p

-    }

-    entry

-  }

-

-  def generateInputNCharactersParser(nChars: Long): Parser[String] = {

-    val anything: Parser[String] = "generateInputNCharactersParser.anything".!!!(""".*""".r)

-    val rNChars = """(?s).{""" + nChars.toString() + """}"""

-    //val firstNChars: Parser[String] = String.format("""(?s).{%s}""", nChars.toString()).r

-    val firstNChars: Parser[String] = "generateInputNCharactersParser.firstNChars".!!!(rNChars.r)

-    val entry = firstNChars //<~ anything // Technically shouldn't need to add anything, we only want the first nChars

-    entry

-  }

-

-  def generateCombinedDelimsParser(localDelims: Set[String], remoteDelims: Set[String]): Parser[String] = {

-    val (localDelimsParser, localDelimsRegex) = this.buildDelims(localDelims)

-    val (remoteDelimsParser, remoteDelimsRegex) = this.buildDelims(remoteDelims)

-    val combinedDelims = remoteDelimsParser ++ localDelimsParser

-    val combinedDelimsParser = this.combineLongest(combinedDelims)

-    combinedDelimsParser

-  }

-

-  def generateCombinedDelimsParser(localDelimsParser: Array[Parser[String]],

-    remoteDelimsParser: Array[Parser[String]]): Parser[String] = {

-    val combinedDelims = remoteDelimsParser ++ localDelimsParser

-    val combinedDelimsParser = this.combineLongest(combinedDelims)

-    combinedDelimsParser

-  }

-

-  def generateLocalDelimsRegex(localDelimsRegex: Array[String]): String = {

-    val newLocalDelimsRegex = "(?s)^(" + combineDelimitersRegex(localDelimsRegex, Array.empty[String]) + ")$"

-    newLocalDelimsRegex

-  }

-

-  def generateIsLocalDelimParser(localDelimsRegex: Array[String]): Parser[String] = {

-    val isLocalDelimsParser: Parser[String] = "generateIsLocalDelimParser.isLocalDelimsParser".!!!(generateLocalDelimsRegex(localDelimsRegex).r)

-    isLocalDelimsParser

-  }

-

-  def generateDelimiter(delims: Set[String]): (Array[Parser[String]], Array[String]) = {

-    val (delimsParser, delimsRegex) = this.buildDelims(delims)

-    (delimsParser, delimsRegex)

-  }

-

-  def generateInputDelimiterParsers(localDelims: Set[String], remoteDelims: Set[String]): (Parser[String], Parser[String], Set[(String, String)]) = {

-    val (localDelimsParser, localDelimsRegex) = this.buildDelims(localDelims)

-    val (remoteDelimsParser, remoteDelimsRegex) = this.buildDelims(remoteDelims)

-    val combinedDelims = localDelimsParser ++ remoteDelimsParser

-    val combinedDelimsParser = this.combineLongest(combinedDelims)

-    val isLocalDelimParser = generateIsLocalDelimParser(localDelimsRegex)

-    //val inputDelimiterParser = combinedDelimsParser <~ opt(EOF) // Should yield longest match of all the delimiters

-    //    val p0 = combinedDelimsParser ~! EOF ^^ { case(p ~ e) => p}

-    //    val p1 = combinedDelimsParser

-    //    val inputDelimiterParser = p0 ||| p1

-    val inputDelimiterParser = combinedDelimsParser ~! opt(EOF) ^^ {

-      case (d ~ _) => d

-    }

-    val isRemoteRegex = combineDelimitersRegex(remoteDelimsRegex, Array.empty)

-    (inputDelimiterParser, isLocalDelimParser, getDelimsRegex(remoteDelims))

-  }

-

-  def generateInputDelimiterParser(localDelims: Set[String], remoteDelims: Set[String]): Parser[String] = {

-    val combinedDelimsParser = generateCombinedDelimsParser(localDelims, remoteDelims)

-    //val entry = combinedDelimsParser <~ opt(EOF) // Should yield longest match of all the delimiters

-    //    val p0 = combinedDelimsParser ~! EOF ^^ { case (p ~ e) => p}

-    //    val p1 = combinedDelimsParser

-    //    val entry = p0 ||| p1

-    //    entry

-    val inputDelimiterParser = combinedDelimsParser ~! opt(EOF) ^^ {

-      case (d ~ _) => d

-    }

-    inputDelimiterParser

-  }

-

-  def generateInputDelimiterParser(localDelims: Array[Parser[String]], remoteDelims: Array[Parser[String]]): Parser[String] = {

-    val combinedDelimsParser = generateCombinedDelimsParser(localDelims, remoteDelims)

-    //val entry = combinedDelimsParser <~ opt(EOF) // Should yield longest match of all the delimiters

-    //    val p0 = combinedDelimsParser ~! EOF ^^ { case (p ~ e) => p}

-    //    val p1 = combinedDelimsParser

-    //    val entry = p0 ||| p1

-    val inputDelimiterParser = combinedDelimsParser ~! opt(EOF) ^^ {

-      case (d ~ _) => d

-    }

-    inputDelimiterParser

-  }

-

-  def generateCharacterRegex(character: String): String = {

-    val characterRegex = convertDFDLLiteralToRegex(character)

-    characterRegex

-  }

-  protected def generateSeparators(separators: Set[String]): (Parser[String], Array[String]) = {

-    val (sepsParser, sepsRegex) = this.buildDelims(separators)

-    val pSeps: Parser[String] = this.combineLongest(sepsParser)

-    (pSeps, sepsRegex)

-  }

-  protected def generateTerminators(terminators: Set[String]): (Parser[String], Array[String]) = {

-    val (termsParser, termsRegex) = this.buildDelims(terminators)

-    val pTerms: Parser[String] = this.combineLongest(termsParser)

-    (pTerms, termsRegex)

-  }

-

-  protected def generateInputWithPadParser(separators: Parser[String], terminators: Parser[String],

-    delimsRegex: String,

-    padCharRegex: String,

-    justification: TextJustificationType.Type,

-    hasDelim: Boolean,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val pSeps: Parser[String] = "generateInputWithPadParser.pSeps".!!!(separators)

-    val pTerms: Parser[String] = "generateInputWithPadParser.pTerms".!!!(terminators)

-

-    //val rPadChar = """(%s*)""".format(padCharRegex)

-    val rPadChar = """(""" + padCharRegex + """*)"""

-    val pPadChar: Parser[String] = "generateInputWithPadParser.pPadChar".!!!(rPadChar.r)

-    //val rBefore = """(?s)(.*?)(?=(%1$s+(%2$s))|(%1$s+\z)|(%2$s)|(\z))""".format(padCharRegex, delimsRegex)

-    val rBefore = """(?s)(.*?)(?=(""" + padCharRegex + """+(""" + delimsRegex + """))|(""" + padCharRegex + """+\z)|(""" + delimsRegex + """)|(\z))"""

-    val pBefore: Parser[String] = "generateInputWithPadParser.pBefore".!!!(rBefore.r)

-    //val rBeforeNoDelims = """(?s)(.*?)(?=(%1$s+\z)|(\z))""".format(padCharRegex)

-    val rBeforeNoDelims = """(?s)(.*?)(?=(""" + padCharRegex + """+\z)|(\z))"""

-    val pBeforeNoDelims: Parser[String] = "generateInputWithPadParser.pBeforeNoDelims".!!!(rBeforeNoDelims.r)

-    //val rBeforeNoPadding = """(?s)((.*?)(?=(%1$s)|(\z)))|(.*)""".format(delimsRegex)

-    val rBeforeNoPadding = """(?s)((.*?)(?=(""" + delimsRegex + """)|(\z)))|(.*)"""

-    val pBeforeNoPadding: Parser[String] = "generateInputWithPadParser.pBeforeNoPadding".!!!(rBeforeNoPadding.r)

-    val rBeforeNoPaddingOrDelims = """(?s)(.*?)(?=(\z))"""

-    val pBeforeNoPaddingOrDelims: Parser[String] = "generateInputWithPadParser.pBeforeNoPaddingOrDelims".!!!(rBeforeNoPaddingOrDelims.r)

-    val pDelims: Parser[String] = "generateInputWithPadParser.pDelims".!!!(pSeps ||| pTerms)

-

-    val paddedContent = "generateInputWithPadParser.paddedContent".!!!(pPadChar ~! pBefore ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }

-    val leftPaddedContent = "generateInputWithPadParser.leftPaddedContent".!!!(pPadChar ~! pBeforeNoPadding) ^^ { case (lp ~ c) => Vector(lp, c) }

-    val rightPaddedContent = "generateInputWithPadParser.rightPaddedContent".!!!(pBefore ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }

-

-    val pFieldAndDelim: Parser[(Vector[String], String)] = (justification, isMissingDelimAllowed, hasDelim) match {

-      case (TextJustificationType.Left, false, true) => {

-        val contentLeftDelimReq = "generateInputWithPadParser.contentLeft_ReqDelims".!!!(rightPaddedContent ~! pDelims) ^^ { case (c ~ d) => (c, d) }

-        contentLeftDelimReq

-      }

-      case (TextJustificationType.Right, false, true) => {

-        val contentRightDelimReq = "generateInputWithPadParser.contentRight_ReqDelims".!!!(leftPaddedContent ~! pDelims) ^^ { case (c ~ d) => (c, d) }

-        contentRightDelimReq

-      }

-      case (TextJustificationType.Center, false, true) => {

-        val contentCenterDelimReq = "generateInputWithPadParser.contentCenter_ReqDelims".!!!(paddedContent ~! pDelims) ^^ { case (c ~ d) => (c, d) }

-        contentCenterDelimReq

-      }

-      case (TextJustificationType.Left, true, true) => {

-        val contentLeft = "generateInputWithPadParser.contentLeft_OptDelims".!!!(rightPaddedContent ~! (pDelims | EOF)) ^^ { case (c ~ d) => (c, d) }

-        contentLeft

-      }

-      case (TextJustificationType.Left, true, false) => {

-        val rightPaddedContent = "generateInputWithPadParser.rightPaddedContent_NoDelims".!!!(pBeforeNoDelims ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }

-        val contentLeft = "generateInputWithPadParser.contentLeft_NoDelims".!!!(rightPaddedContent ~! (EOF)) ^^ { case (c ~ d) => (c, d) }

-        contentLeft

-      }

-      case (TextJustificationType.Right, true, true) => {

-        val contentRight = "generateInputWithPadParser.contentRight_OptDelims".!!!(leftPaddedContent ~! (pDelims | EOF)) ^^ { case (c ~ d) => (c, d) }

-        contentRight

-      }

-      case (TextJustificationType.Right, true, false) => {

-        val leftPaddedContent = "generateInputWithPadParser.leftPaddedContent_NoDelims".!!!(pPadChar ~! pBeforeNoDelims) ^^ { case (lp ~ c) => Vector(lp, c) }

-        val contentRight = "generateInputWithPadParser.contentRight_NoDelims".!!!(leftPaddedContent ~! (EOF)) ^^ { case (c ~ d) => (c, d) }

-        contentRight

-      }

-      case (TextJustificationType.Center, true, true) => {

-        val contentCenter = "generateInputWithPadParser.contentCenter_OptDelims".!!!(paddedContent ~! (pDelims | EOF)) ^^ { case (c ~ d) => (c, d) }

-        contentCenter

-      }

-      case (TextJustificationType.Center, true, false) => {

-        val paddedContent = "generateInputWithPadParser.paddedContent_NoDelims".!!!(pPadChar ~! pBeforeNoDelims ~ pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }

-        val contentCenter = "generateInputWithPadParser.contentCenter_NoDelims".!!!(paddedContent ~! (EOF)) ^^ { case (c ~ d) => (c, d) }

-        contentCenter

-      }

-      case _ => Assert.invariantFailed("not one of the combinations.") // return failedResult

-    }

-    pFieldAndDelim

-  }

-

-  protected def generateInputNoPadParser(separators: Parser[String], terminators: Parser[String],

-    delimsRegex: String, hasDelim: Boolean,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val pSeps: Parser[String] = "generateInputNoPadParser.pSeps".!!!(separators)

-    val pTerms: Parser[String] = "generateInputNoPadParser.pTerms".!!!(terminators)

-

-    //val rBeforeNoPadding = """(?s)((.*?)(?=(%1$s)|(\z)))|(.*)"""

-    //val pBeforeNoPadding: Parser[String] = rBeforeNoPadding.format(delimsRegex).r

-    val rBeforeNoPadding = """(?s)((.*?)(?=(""" + delimsRegex + """)|(\z)))|(.*)"""

-    val pBeforeNoPadding: Parser[String] = "generateInputNoPadParser.pBeforeNoPadding".!!!(rBeforeNoPadding.r)

-

-    val rBeforeNoPaddingOrDelims = """(?s)(.*?)(?=(\z))"""

-    val pBeforeNoPaddingOrDelims: Parser[String] = "generateInputNoPadParser.pBeforeNoPaddingOrDelims".!!!(rBeforeNoPaddingOrDelims.r)

-    val pDelims: Parser[String] = "generateInputNoPadParser.pDelims".!!!(pSeps ||| pTerms)

-

-    val pFieldAndDelim: Parser[(Vector[String], String)] = (isMissingDelimAllowed, hasDelim) match {

-      case (false, true) => {

-        val contentDelimReq = "generateInputNoPadParser.content_ReqDelims".!!!(pBeforeNoPadding ~! pDelims) ^^ { case (c ~ d) => (Vector(c), d) }

-        contentDelimReq

-      }

-      case (true, true) => {

-        val content = "generateInputNoPadParser.content_OptDelims".!!!(pBeforeNoPadding ~! (pDelims | EOF)) ^^ { case (c ~ d) => (Vector(c), d) }

-        content

-      }

-      case (true, false) => {

-        val content = "generateInputNoPadParser.content_NoDelims".!!!(pBeforeNoPaddingOrDelims ~! (EOF)) ^^ { case (c ~ d) => (Vector(c), d) }

-        content

-      }

-      case _ => Assert.invariantFailed("impossible combination") // return failedResult

-    }

-    pFieldAndDelim

-  }

-

-  def generateInputParser(separators: Set[String], terminators: Set[String],

-    justification: TextJustificationType.Type,

-    padChar: String,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-    val hasDelim: Boolean = separators.size > 0 || terminators.size > 0

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)

-    val padCharRegex = generateCharacterRegex(padChar);

-

-    justification match {

-      case TextJustificationType.None => return generateInputNoPadParser(pSeps, pTerms,

-        delimsRegex, hasDelim, isMissingDelimAllowed)

-      case _ => return generateInputWithPadParser(pSeps, pTerms, delimsRegex, padCharRegex,

-        justification, hasDelim, isMissingDelimAllowed)

-    }

-  }

-

-  def generateInputParser2(pSeps: Parser[String], pTerms: Parser[String],

-    sepsRegex: Array[String], termsRegex: Array[String], hasDelim: Boolean,

-    justification: TextJustificationType.Type, padChar: String,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)

-    val padCharRegex = generateCharacterRegex(padChar);

-

-    justification match {

-      case TextJustificationType.None => return generateInputNoPadParser(pSeps, pTerms,

-        delimsRegex, hasDelim, isMissingDelimAllowed)

-      case _ => return generateInputWithPadParser(pSeps, pTerms, delimsRegex, padCharRegex,

-        justification, hasDelim, isMissingDelimAllowed)

-    }

-  }

-

-  protected def generateEscapeBlockWithPadParser(separators: Parser[String], terminators: Parser[String],

-    escapeBlockStartRegex: String, escapeBlockEndRegex: String,

-    escapeEscapeCharacterRegex: String,

-    padCharRegex: String,

-    justification: TextJustificationType.Type,

-    hasEscEsc: Boolean,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val pEscape: Parser[String] = "generateEscapeBlockWithPadParser.pEscape".!!!(escapeEscapeCharacterRegex.r)

-    val pBlockStart: Parser[String] = "generateEscapeBlockWithPadParser.pBlockStart".!!!(escapeBlockStartRegex.r)

-    val pBlockEnd: Parser[String] = "generateEscapeBlockWithPadParser.pBlockEnd".!!!(escapeBlockEndRegex.r)

-

-    val pSeps: Parser[String] = "generateEscapeBlockWithPadParser.pSeps".!!!(separators)

-    val pTerms: Parser[String] = "generateEscapeBlockWithPadParser.pTerms".!!!(terminators)

-    val pDelims: Parser[String] = "generateEscapeBlockWithPadParser.pDelims".!!!(pSeps ||| pTerms)

-

-    //    val rUnescapedBlockStart = if (hasEscEsc) """(?<!%1$s)%2$s""" else """%2$s"""

-    //    val pUnescapedBlockStart: Parser[String] = rUnescapedBlockStart.format(escapeEscapeCharacterRegex, escapeBlockStartRegex).r

-    val rUnescapedBlockStart = if (hasEscEsc) """(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeBlockStartRegex + """""" else escapeBlockStartRegex

-    val pUnescapedBlockStart: Parser[String] = "generateEscapeBlockWithPadParser.pUnescapedBlockStart".!!!(rUnescapedBlockStart.r)

-    //    val rUnescapedBlockEnd = if (hasEscEsc) """(?<!%1$s)%2$s""" else """%2$s"""

-    //    val pUnescapedBlockEnd: Parser[String] = rUnescapedBlockEnd.format(escapeEscapeCharacterRegex, escapeBlockEndRegex).r

-    val rUnescapedBlockEnd = if (hasEscEsc) """(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeBlockEndRegex + """""" else escapeBlockEndRegex

-    val pUnescapedBlockEnd: Parser[String] = "generateEscapeBlockWithPadParser.pUnescapedBlockEnd".!!!(rUnescapedBlockEnd.r)

-    val rBeforeUnescapedBlockEnd = """(?s)(.*?)(?=(""" + rUnescapedBlockEnd + """))"""

-    //    val pBeforeUnescapedBlockEnd: Parser[String] = rBeforeUnescapedBlockEnd.format(escapeEscapeCharacterRegex, escapeBlockEndRegex).r

-    val pBeforeUnescapedBlockEnd: Parser[String] = "generateEscapeBlockWithPadParser.pBeforeUnescapedBlockEnd".!!!(rBeforeUnescapedBlockEnd.r)

-    val blockedContent: Parser[Vector[String]] = "generateEscapeBlockWithPadParser.blockedContent".!!!(pUnescapedBlockStart ~! pBeforeUnescapedBlockEnd ~! pUnescapedBlockEnd) ^^ { case (bs ~ c ~ be) => Vector(bs, c, be) }

-

-    //    val rPadChar = """(%s*)"""

-    //    val pPadChar: Parser[String] = rPadChar.format(padCharRegex).r

-    val rPadChar = """(""" + padCharRegex + """*)"""

-    val pPadChar: Parser[String] = "generateEscapeBlockWithPadParser.pPadChar".!!!(rPadChar.r)

-    val paddedBlockedContent: Parser[Vector[String]] = "generateEscapeBlockWithPadParser.paddedBlockedContent".!!!(pPadChar ~! blockedContent ~! pPadChar) ^^ { case (lp ~ bc ~ rp) => Vector(lp) ++ bc ++ Vector(rp) }

-    val leftPaddedBlockedContent: Parser[Vector[String]] = "generateEscapeBlockWithPadParser.leftPaddedBlockedContent".!!!(pPadChar ~! blockedContent) ^^ { case (lp ~ bc) => Vector(lp) ++ bc }

-    val rightPaddedBlockedContent: Parser[Vector[String]] = "generateEscapeBlockWithPadParser.rightPaddedBlockedContent".!!!(blockedContent ~! pPadChar) ^^ { case (bc ~ rp) => bc ++ Vector(rp) }

-

-    val pFieldAndDelim: Parser[(Vector[String], String)] = (justification -> isMissingDelimAllowed) match {

-      case (TextJustificationType.Left, false) => {

-        val contentLeftDelimReq: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentLeft_ReqDelims".!!!(rightPaddedBlockedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }

-        contentLeftDelimReq

-      }

-      case (TextJustificationType.Right, false) => {

-        val contentRightDelimReq: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentRight_ReqDelims".!!!(leftPaddedBlockedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }

-        contentRightDelimReq

-      }

-      case (TextJustificationType.Center, false) => {

-        val contentCenterDelimReq: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentCenter_ReqDelims".!!!(paddedBlockedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }

-        contentCenterDelimReq

-      }

-      case (TextJustificationType.Left, true) => {

-        val contentLeft: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentLeft_OptDelims".!!!(rightPaddedBlockedContent ~! (pDelims | EOF)) ^^ { case (bc ~ d) => (bc -> d) }

-        contentLeft

-      }

-      case (TextJustificationType.Right, true) => {

-        val contentRight: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentRight_OptDelims".!!!(leftPaddedBlockedContent ~! (pDelims | EOF)) ^^ { case (bc ~ d) => (bc -> d) }

-        contentRight

-      }

-      case (TextJustificationType.Center, true) => {

-        val contentCenter: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentCenter_OptDelims".!!!(paddedBlockedContent ~! (pDelims | EOF)) ^^ { case (bc ~ d) => (bc -> d) }

-        contentCenter

-      }

-      case (TextJustificationType.None, _) => {

-        Assert.invariantFailed("shouldn't be None. We got to this match based on knowing it is not None")

-      }

-    }

-    pFieldAndDelim

-  }

-

-  protected def generateEscapeBlockNoPadParser(separators: Parser[String], terminators: Parser[String],

-    escapeBlockStartRegex: String, escapeBlockEndRegex: String,

-    escapeEscapeCharacterRegex: String = "",

-    hasEscEsc: Boolean,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val pEscape: Parser[String] = "generateEscapeBlockNoPadParser.pEscape".!!!(escapeEscapeCharacterRegex.r)

-    val pBlockStart: Parser[String] = "generateEscapeBlockNoPadParser.pBlockStart".!!!(escapeBlockStartRegex.r)

-    val pBlockEnd: Parser[String] = "generateEscapeBlockNoPadParser.pBlockEnd".!!!(escapeBlockEndRegex.r)

-

-    val pSeps: Parser[String] = "generateEscapeBlockNoPadParser.pSeps".!!!(separators)

-    val pTerms: Parser[String] = "generateEscapeBlockNoPadParser.pTerms".!!!(terminators)

-    val pDelims: Parser[String] = "generateEscapeBlockNoPadParser.pDelims".!!!(pSeps ||| pTerms)

-

-    //    val rBeforeUnescapedBlockEnd = """(?s)(.*?)(?=(""" + rUnescapedBlockEnd + """))"""

-    //    val pBeforeUnescapedBlockEnd: Parser[String] = rBeforeUnescapedBlockEnd.format(escapeEscapeCharacterRegex, escapeBlockEndRegex).r

-    //    val rUnescapedBlockStart = if (hasEscEsc) """(?<!%1$s)%2$s""" else """%2$s"""

-    //    val pUnescapedBlockStart: Parser[String] = rUnescapedBlockStart.format(escapeEscapeCharacterRegex, escapeBlockStartRegex).r

-    val rUnescapedBlockStart = if (hasEscEsc) """(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeBlockStartRegex else escapeBlockStartRegex

-    val pUnescapedBlockStart: Parser[String] = "generateEscapeBlockNoPadParser.pUnescapedBlockStart".!!!(rUnescapedBlockStart.r)

-

-    //    val rUnescapedBlockEnd = if (hasEscEsc) """(?<!%1$s)%2$s""" else """%2$s"""

-    //    val pUnescapedBlockEnd: Parser[String] = rUnescapedBlockEnd.format(escapeEscapeCharacterRegex, escapeBlockEndRegex).r

-    val rUnescapedBlockEnd = if (hasEscEsc) """(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeBlockEndRegex else escapeBlockEndRegex

-    val pUnescapedBlockEnd: Parser[String] = "generateEscapeBlockNoPadParser.pUnescapedBlockEnd".!!!(rUnescapedBlockEnd.r)

-    val rBeforeUnescapedBlockEnd = """(?s)(.*?)(?=(""" + rUnescapedBlockEnd + """))"""

-    val pBeforeUnescapedBlockEnd: Parser[String] = "generateEscapeBlockNoPadParser.pBeforeUnescapedBlockEnd".!!!(rBeforeUnescapedBlockEnd.r)

-    val blockedContent: Parser[Vector[String]] = "generateEscapeBlockNoPadParser.blockedContent".!!!(pUnescapedBlockStart ~! pBeforeUnescapedBlockEnd ~! pUnescapedBlockEnd) ^^ { case (bs ~ c ~ be) => Vector(bs, c, be) }

-

-    val pFieldAndDelim: Parser[(Vector[String], String)] = isMissingDelimAllowed match {

-      case false => {

-        val contentDelimReq: Parser[(Vector[String], String)] = "generateEscapeBlockNoPadParser.content_ReqDelims".!!!(blockedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }

-        contentDelimReq

-      }

-      case true => {

-        val content: Parser[(Vector[String], String)] = "generateEscapeBlockNoPadParser.content_OptDelims".!!!(blockedContent ~! (pDelims | EOF)) ^^ { case (bc ~ d) => (bc -> d) }

-        content

-      }

-    }

-    pFieldAndDelim

-  }

-

-  def generateEscapeBlockParser(separators: Set[String], terminators: Set[String],

-    escapeBlockStart: String, escapeBlockEnd: String,

-    escapeEscapeCharacter: String = "",

-    justification: TextJustificationType.Type,

-    padChar: String,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0

-    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)

-    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)

-    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)

-    val padCharRegex = generateCharacterRegex(padChar);

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-

-    justification match {

-      case TextJustificationType.None => return this.generateEscapeBlockNoPadParser(pSeps, pTerms,

-        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)

-      case _ => return this.generateEscapeBlockWithPadParser(pSeps, pTerms,

-        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)

-    }

-  }

-

-  def generateEscapeBlockParsers(separators: Set[String], terminators: Set[String],

-    escapeBlockStart: String, escapeBlockEnd: String,

-    escapeEscapeCharacter: String = "",

-    justification: TextJustificationType.Type,

-    padChar: String,

-    isMissingDelimAllowed: Boolean = true): (Parser[(Vector[String], String)], Parser[String], Parser[String], Array[String], Array[String]) = {

-

-    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0

-    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)

-    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)

-    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)

-    val padCharRegex = generateCharacterRegex(padChar);

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-

-    val escapeBlockParser = justification match {

-      case TextJustificationType.None => this.generateEscapeBlockNoPadParser(pSeps, pTerms,

-        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)

-      case _ => this.generateEscapeBlockWithPadParser(pSeps, pTerms,

-        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)

-    }

-    (escapeBlockParser, pSeps, pTerms, sepsRegex, termsRegex)

-  }

-

-  lazy val emptyParser = { parserAlwaysFail("")("empty delimiter list") }

-

-  def generateEscapeBlockParsers2(delims: Parser[String],

-    escapeBlockStart: String, escapeBlockEnd: String,

-    escapeEscapeCharacter: String = "",

-    justification: TextJustificationType.Type,

-    padChar: String,

-    isMissingDelimAllowed: Boolean = true): (Parser[(Vector[String], String)], String, String) = {

-

-    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0

-    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)

-    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)

-    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)

-    val padCharRegex = generateCharacterRegex(padChar);

-

-    val escapeBlockParser = justification match {

-      case TextJustificationType.None => this.generateEscapeBlockNoPadParser(emptyParser, delims,

-        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)

-      case _ => this.generateEscapeBlockWithPadParser(emptyParser, delims,

-        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)

-    }

-    (escapeBlockParser, escapeBlockEndRegex, escapeEscapeRegex)

-  }

-

-  protected def generateEscapeCharacterDiffWithPadParser(separators: Parser[String], terminators: Parser[String],

-    escapeCharacterRegex: String, escapeEscapeCharacterRegex: String, padCharRegex: String,

-    delimsRegex: String,

-    justification: TextJustificationType.Type, hasEscEsc: Boolean, hasDelim: Boolean,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val pSeps: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pSeps".!!!(separators)

-    val pTerms: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pTerms".!!!(terminators)

-    val pDelims: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pDelims".!!!(pSeps ||| pTerms)

-

-    val pEscape: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pEscape".!!!(escapeCharacterRegex.r)

-    val pEscapeEscape: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pEscapeEscape".!!!(escapeEscapeCharacterRegex.r)

-    val pEscapedEscape = "generateEscapeCharacterDiffWithPadParser.pEscapedEscape".!!!(pEscapeEscape ~! pEscape)

-

-    val pEscapeEscapeFollowedByEscapeFollowedByDelim = "generateEscapeCharacterDiffWithPadParser.pEscapeEscapeFollowedByEscapeFollowedByDelim".!!!((pEscapeEscape ~! pEscape) ~! pDelims) ^^ { case (eses ~ es ~ d) => d }

-    val pDelimNotPrecededByEscape = "generateEscapeCharacterDiffWithPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~! pDelims) ^^ { case (notEsc ~ d) => d }

-    val pEscEscFollowedByDelim = "generateEscapeCharacterDiffWithPadParser.pEscEscFollowedByDelim".!!!(pEscapeEscape ~! pDelims) ^^ { case (eses ~ d) => d }

-    //val pUnescapedDelims = ((pEscapeEscape ~! pEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | (pEscapeEscape ~> pDelims) | pDelims

-    val pUnescapedDelims = "generateEscapeCharacterDiffWithPadParser.pUnescapedDelims".!!!(pEscapeEscapeFollowedByEscapeFollowedByDelim | pDelimNotPrecededByEscape | pEscEscFollowedByDelim | pDelims)

-

-    //val rPadChar = """(%s*)"""

-    val rPadChar = """(""" + padCharRegex + """*)"""

-    val pPadChar: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pPadChar".!!!(rPadChar.r)

-    //val rLeftPadChar = """(%1$s*)(?=([^%1$s]))""" // LeftPad precedes non pad characters

-    val rLeftPadChar = """(""" + padCharRegex + """*)(?=([^""" + padCharRegex + """]))""" // LeftPad precedes non pad characters

-    val pLeftPadChar: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pLeftPadChar".!!!(rLeftPadChar.format(padCharRegex).r)

-

-    // Content is anything until:

-    // 1. Padding ~ delimiter

-    // 2. unescaped delimiter

-    // 3. Optional Padding ~ EndOfData

-    //    val rBefore = """(?s)(.*?)(?=(%4$s+%3$s)|((?<!(?<!%1$s)%2$s)%3$s)|(%4$s*\z))"""

-    //    val rBeforeNoEscEsc = """(?s)(.*?)(?=(%3$s+%2$s)|((?<!%1$s)%2$s)|(%3$s*\z))"""

-    //    val pBefore: Parser[String] = {

-    //      if (hasEscEsc) rBefore.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex, padCharRegex).r

-    //      else rBeforeNoEscEsc.format(escapeCharacterRegex, delimsRegex, padCharRegex).r

-    //    }

-

-    val pBefore: Parser[String] = {

-      if (hasEscEsc) {

-        val rBefore = """(?s)(.*?)(?=(""" + padCharRegex + """+""" + delimsRegex + """)|((?<!(?<!""" +

-          escapeEscapeCharacterRegex + """)""" + escapeCharacterRegex + """)""" + delimsRegex + """)|(""" + padCharRegex + """*\z))"""

-        "generateEscapeCharacterDiffWithPadParser.pBeforeWithEscEsc".!!!(rBefore.r)

-      } else {

-        val rBeforeNoEscEsc = """(?s)(.*?)(?=(""" + padCharRegex + """+""" + delimsRegex + """)|((?<!""" +

-          escapeCharacterRegex + """)""" + delimsRegex + """)|(""" + padCharRegex + """*\z))"""

-        "generateEscapeCharacterDiffWithPadParser.BeforeNoEscEsc".!!!(rBeforeNoEscEsc.r)

-      }

-    }

-

-    //    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=(?:(?<!(?<!%1$s)%2$s)%3$s)|\z)"""

-    //    val rBeforeIgnoreTrailingPaddingNoEscEsc = """(?s)(.*?)(?=(?:(?<!%1$s)%2$s)|\z)"""

-    //    val pBeforeIgnoreTrailingPadding: Parser[String] = {

-    //      if (hasEscEsc) rBeforeIgnoreTrailingPadding.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex).r

-    //      else rBeforeIgnoreTrailingPaddingNoEscEsc.format(escapeCharacterRegex, delimsRegex).r

-    //    }

-

-    val pBeforeIgnoreTrailingPadding: Parser[String] = {

-      if (hasEscEsc) {

-        val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=(?:(?<!(?<!""" + escapeEscapeCharacterRegex + """)""" +

-          escapeCharacterRegex + """)""" + delimsRegex + """)|\z)"""

-        "generateEscapeCharacterDiffWithPadParser.BeforeIgnoreTrailingPadding".!!!(rBeforeIgnoreTrailingPadding.r)

-      } else {

-        val rBeforeIgnoreTrailingPaddingNoEscEsc = """(?s)(.*?)(?=(?:(?<!""" + escapeCharacterRegex + """)""" +

-          delimsRegex + """)|\z)"""

-        "generateEscapeCharacterDiffWithPadParser.BeforeIgnoreTrailingPaddingNoEscEsc".!!!(rBeforeIgnoreTrailingPaddingNoEscEsc.r)

-      }

-    }

-

-    //    val rBeforeNoPadding = """(?s)(.*?)(?=((?<!(?<!%1$s)%2$s)%3$s)|(\z))"""

-    //    val rBeforeNoPaddingNoEscEsc = """(?s)(.*?)(?=((?<!%1$s)%2$s)|(\z))"""

-    //    val pBeforeNoPadding: Parser[String] = {

-    //      if (hasEscEsc) rBeforeNoPadding.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex).r

-    //      else rBeforeNoPaddingNoEscEsc.format(escapeCharacterRegex, delimsRegex).r

-    //    }

-

-    val pBeforeNoPadding: Parser[String] = {

-      if (hasEscEsc) {

-        val rBeforeNoPadding = """(?s)(.*?)(?=((?<!(?<!""" + escapeEscapeCharacterRegex + """)""" +

-          escapeCharacterRegex + """)""" + delimsRegex + """)|(\z))"""

-        "generateEscapeCharacterDiffWithPadParser.BeforeNoPadding".!!!(rBeforeNoPadding.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex).r)

-      } else {

-        val rBeforeNoPaddingNoEscEsc = """(?s)(.*?)(?=((?<!""" + escapeCharacterRegex + """)""" +

-          delimsRegex + """)|(\z))"""

-        "generateEscapeCharacterDiffWithPadParser.BeforeNoPaddingNoEscEsc".!!!(rBeforeNoPaddingNoEscEsc.format(escapeCharacterRegex, delimsRegex).r)

-      }

-    }

-

-    val rBeforeNoPaddingOrDelims = """(?s)(.*?)(?=(\z))"""

-    val pBeforeNoPaddingOrDelims: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pBeforeNoPaddingOrDelims".!!!(rBeforeNoPaddingOrDelims.r)

-

-    //    val rBeforeNoDelims = """(?s)(.*?)(?=(%1$s*\z))"""

-    //    val pBeforeNoDelims: Parser[String] = rBeforeNoDelims.format(padCharRegex).r

-    val rBeforeNoDelims = """(?s)(.*?)(?=(""" + padCharRegex + """*\z))"""

-    val pBeforeNoDelims: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pBeforeNoDelims".!!!(rBeforeNoDelims.r)

-

-    val paddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.paddedContent".!!!(pLeftPadChar ~! pBefore ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }

-    val leftPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.leftPaddedContent".!!!(pLeftPadChar ~! pBefore) ^^ { case (lp ~ c) => Vector(lp, c) }

-    val rightPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.rightPaddedContent".!!!(pBefore ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }

-

-    val pFieldAndDelim: Parser[(Vector[String], String)] = (justification, isMissingDelimAllowed, hasDelim) match {

-      case (TextJustificationType.Left, false, true) => {

-        val contentLeftDelimReq: Parser[(Vector[String], String)] = "generateEscapeCharacterDiffWithPadParser.contentLeft_ReqDelims".!!!(rightPaddedContent ~! pUnescapedDelims) ^^ { case (c ~ d) => (c, d) }

-        contentLeftDelimReq

-      }

-      case (TextJustificationType.Right, false, true) => {

-        val contentRightDelimReq: Parser[(Vector[String], String)] = "generateEscapeCharacterDiffWithPadParser.contentRight_ReqDelims".!!!(leftPaddedContent ~! pUnescapedDelims) ^^ { case (c ~ d) => (c, d) }

-        contentRightDelimReq

-      }

-      case (TextJustificationType.Center, false, true) => {

-        val contentCenterDelimReq: Parser[(Vector[String], String)] = "generateEscapeCharacterDiffWithPadParser.contentCenter_ReqDelims".!!!(paddedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }

-        contentCenterDelimReq

-      }

-      case (TextJustificationType.Left, true, true) => {

-        val contentLeft: Parser[(Vector[String], String)] = rightPaddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterDiffWithPadParser.contentLeft_OptDelims".!!!(contentLeft)

-      }

-      case (TextJustificationType.Left, true, false) => {

-        val rightPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.rightPaddedContent_NoDelims".!!!(pBeforeNoDelims ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }

-        val contentLeft: Parser[(Vector[String], String)] = rightPaddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterDiffWithPadParser.contentLeft_NoDelims".!!!(contentLeft)

-      }

-      case (TextJustificationType.Right, true, true) => {

-        val contentRight: Parser[(Vector[String], String)] = leftPaddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterDiffWithPadParser.contentRight_OptDelims".!!!(contentRight)

-      }

-      case (TextJustificationType.Right, true, false) => {

-        val leftPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.leftPaddedContent_NoDelims".!!!(pLeftPadChar ~! pBeforeNoDelims) ^^ { case (lp ~ c) => Vector(lp, c) }

-        val contentRight: Parser[(Vector[String], String)] = leftPaddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterDiffWithPadParser.contentRight_NoDelims".!!!(contentRight)

-      }

-      case (TextJustificationType.Center, true, true) => {

-        val contentCenter: Parser[(Vector[String], String)] = paddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterDiffWithPadParser.contentCenter_OptDelims".!!!(contentCenter)

-      }

-      case (TextJustificationType.Center, true, false) => {

-        val paddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.paddedContent_NoDelims".!!!(pLeftPadChar ~! pBeforeNoDelims ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }

-        val contentCenter: Parser[(Vector[String], String)] = paddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterDiffWithPadParser.contentCenter_NoDelims".!!!(contentCenter)

-      }

-      case _ => Assert.invariantFailed("illegal combination") //return failedResult

-    }

-    pFieldAndDelim

-  }

-

-  protected def generateEscapeCharacterDiffNoPadParser(separators: Parser[String], terminators: Parser[String],

-    escapeCharacterRegex: String, escapeEscapeCharacterRegex: String, delimsRegex: String,

-    hasEscEsc: Boolean, hasDelim: Boolean,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val pSeps: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pSeps".!!!(separators)

-    val pTerms: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pTerms".!!!(terminators)

-    val pDelims: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pDelims".!!!(pSeps ||| pTerms)

-

-    val pEscape: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pEscape".!!!(escapeCharacterRegex.r)

-    val pEscapeEscape: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pEscapeEscape".!!!(escapeEscapeCharacterRegex.r)

-    val pEscapedEscape = "generateEscapeCharacterDiffNoPadParser.pEscapedEscape".!!!(pEscapeEscape ~! pEscape)

-    //val pUnescapedDelims = ((pEscapeEscape ~ pEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | (pEscapeEscape ~> pDelims) | pDelims

-    //    val pUnescapedDelims = {

-    //      if (hasEscEsc) { ((pEscapeEscape ~! pEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | (pEscapeEscape ~> pDelims) | pDelims }

-    //      else { (not(pEscape) ~> pDelims) | pDelims }

-    //    }

-

-    // TODO: Same as generateEscapeCharacterDiffWithPadParser, look at generating this separately and passing in

-    //val pEscapeEscapeFollowedByEscapeFollowedByDelim = ((pEscapeEscape ~! pEscape) ~! pDelims) ^^ { case (eses ~ es ~ d) => d }

-    val pDelimNotPrecededByEscape = "generateEscapeCharacterDiffNoPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~! pDelims) ^^ { case (notEsc ~ d) => d }

-    //val pEscEscFollowedByDelim = (pEscapeEscape ~! pDelims) ^^ { case (eses ~ d) => d }

-    //val pUnescapedDelims = pEscapeEscapeFollowedByEscapeFollowedByDelim | pDelimNotPrecededByEscape | pEscEscFollowedByDelim | pDelims

-    val pUnescapedDelims = "generateEscapeCharacterDiffNoPadParser.pUnescapedDelims".!!! {

-      if (hasEscEsc) {

-        // Seems like this is a case where backtracking has to be allowed for pDelimNotPrecededByEscape

-        val pEscapeEscapeFollowedByEscapeFollowedByDelim = "generateEscapeCharacterDiffNoPadParser.pEscapeEscapeFollowedByEscapeFollowedByDelim".!!!((pEscapeEscape ~! pEscape) ~! pDelims) ^^ { case (eses ~ es ~ d) => d }

-        val pDelimNotPrecededByEscape = "generateEscapeCharacterDiffNoPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~ pDelims) ^^ { case (notEsc ~ d) => d }

-        val pEscEscFollowedByDelim = "generateEscapeCharacterDiffNoPadParser.pEscEscFollowedByDelim".!!!(pEscapeEscape ~! pDelims) ^^ { case (eses ~ d) => d }

-        pEscapeEscapeFollowedByEscapeFollowedByDelim | pDelimNotPrecededByEscape | pEscEscFollowedByDelim | pDelims

-      } else {

-        pDelimNotPrecededByEscape | pDelims

-      }

-    }

-

-    //    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=(?:(?<!(?<!%1$s)%2$s)%3$s)|\z)"""

-    //    val rBeforeIgnoreTrailingPaddingNoEscEsc = """(?s)(.*?)(?=(?:(?<!%1$s)%2$s)|\z)"""

-    //    val pBeforeIgnoreTrailingPadding: Parser[String] = {

-    //      if (hasEscEsc) rBeforeIgnoreTrailingPadding.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex).r

-    //      else rBeforeIgnoreTrailingPaddingNoEscEsc.format(escapeCharacterRegex, delimsRegex).r

-    //    }

-

-    val pBeforeIgnoreTrailingPadding: Parser[String] = {

-      if (hasEscEsc) {

-        val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=(?:(?<!(?<!""" + escapeEscapeCharacterRegex + """)""" +

-          escapeCharacterRegex + """)""" + delimsRegex + """)|\z)"""

-        "generateEscapeCharacterDiffNoPadParser.BeforeIgnoreTrailingPadding".!!!(rBeforeIgnoreTrailingPadding.r)

-      } else {

-        val rBeforeIgnoreTrailingPaddingNoEscEsc = """(?s)(.*?)(?=(?:(?<!""" + escapeCharacterRegex + """)""" +

-          delimsRegex + """)|\z)"""

-        "generateEscapeCharacterDiffNoPadParser.BeforeIgnoreTrailingPaddingNoEscEsc".!!!(rBeforeIgnoreTrailingPaddingNoEscEsc.r)

-      }

-    }

-

-    val pBeforeNoPadding: Parser[String] = {

-      if (hasEscEsc) {

-        val rBeforeNoPadding = """(?s)(.*?)(?=((?<!(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeCharacterRegex + """)""" + delimsRegex + """)|(\z))"""

-        "generateEscapeCharacterDiffNoPadParser.BeforeNoPadding".!!!(rBeforeNoPadding.r)

-      } else {

-        val rBeforeNoPaddingNoEscEsc = """(?s)(.*?)(?=((?<!""" + escapeCharacterRegex + """)""" + delimsRegex + """)|(\z))"""

-        "generateEscapeCharacterDiffNoPadParser.BeforeNoPaddingNoEscEsc".!!!(rBeforeNoPaddingNoEscEsc.r)

-      }

-    }

-

-    val rBeforeNoPaddingOrDelims = """(?s)(.*?)(?=(\z))"""

-    val pBeforeNoPaddingOrDelims: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pBeforeNoPaddingOrDelims".!!!(rBeforeNoPaddingOrDelims.r)

-

-    val rBeforeNoDelims = """(?s)(.*?)(?=(\z))"""

-    val pBeforeNoDelims: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pBeforeNoDelims".!!!(rBeforeNoDelims.r)

-

-    val pFieldAndDelim: Parser[(Vector[String], String)] = (isMissingDelimAllowed, hasDelim) match {

-      case (false, true) => {

-        val contentDelimReq: Parser[(Vector[String], String)] = pBeforeNoPadding ~! pUnescapedDelims ^^ { case (b ~ d) => (Vector(b) -> d) }

-        "generateEscapeCharacterDiffNoPadParser.content_ReqDelims".!!!(contentDelimReq)

-      }

-      case (true, true) => {

-        val content: Parser[(Vector[String], String)] = pBeforeIgnoreTrailingPadding ~! (pUnescapedDelims | EOF) ^^ { case (b ~ d) => (Vector(b) -> d) }

-        "generateEscapeCharacterDiffNoPadParser.content_OptDelims".!!!(content)

-      }

-      case (true, false) => {

-        val content: Parser[(Vector[String], String)] = pBeforeNoPaddingOrDelims ~! (EOF) ^^ { case (b ~ d) => (Vector(b) -> d) }

-        "generateEscapeCharacterDiffNoPadParser.content_NoDelims".!!!(content)

-      }

-      case _ => Assert.invariantFailed() // return failedResult

-    }

-    pFieldAndDelim

-  }

-

-  protected def generateEscapeCharacterSameNoPadParser(separators: Parser[String], terminators: Parser[String],

-    escapeCharacterRegex: String, delimsRegex: String, hasDelim: Boolean,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val pSeps: Parser[String] = "generateEscapeCharacterSameNoPadParser.pSeps".!!!(separators)

-    val pTerms: Parser[String] = "generateEscapeCharacterSameNoPadParser.pTerms".!!!(terminators)

-    val pDelims: Parser[String] = "generateEscapeCharacterSameNoPadParser.pDelims".!!!(pSeps ||| pTerms)

-

-    val pEscape: Parser[String] = "generateEscapeCharacterSameNoPadParser.pEscape".!!!(escapeCharacterRegex.r)

-    val pEscapedEscape = "generateEscapeCharacterSameNoPadParser.pEscapedEscape".!!!(pEscape ~! pEscape) ^^ { case (e1 ~ e2) => (e1 + e2) } // concatenate escapes

-

-    // Parser captures and creates a string representation of the escapes

-    val pEscapes = "generateEscapeCharacterSameNoPadParser.pEscapes".!!!((pEscapedEscape.*) ~! opt(pEscape)) ^^ {

-      case (l ~ None) => l.mkString

-      case (l ~ Some(esc)) => l.mkString + esc

-    }

-

-    //    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=((?<!%1$s)((%1$s%1$s)*)(%2$s))|(\z))"""

-    //    val pBeforeIgnoreTrailingPadding: Parser[String] = rBeforeIgnoreTrailingPadding.format(escapeCharacterRegex, delimsRegex).r

-    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=((?<!""" + escapeCharacterRegex + """)((""" + escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))|(\z))"""

-    val pBeforeIgnoreTrailingPadding: Parser[String] = "generateEscapeCharacterSameNoPadParser.pBeforeIgnoreTrailingPadding".!!!(rBeforeIgnoreTrailingPadding.r)

-

-    //    val rBeforeNoPadding = """(?s)(.*?)(?=(""" + // Give me everything from this point until...

-    //      """((?<!%1$s)((%1$s%1$s)*)(%2$s))""" + // unescaped delimiter

-    //      """|""" + // OR

-    //      """\z))""" // End of data/file

-    //    val pBeforeNoPadding: Parser[String] = rBeforeNoPadding.format(escapeCharacterRegex, delimsRegex).r

-    val rBeforeNoPadding = """(?s)(.*?)(?=(""" + // Give me everything from this point until...

-      """((?<!""" + escapeCharacterRegex + """)((""" +

-      escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))""" + // unescaped delimiter

-      """|""" + // OR

-      """\z))""" // End of data/file

-

-    val pBeforeNoPadding: Parser[String] = "generateEscapeCharacterSameNoPadParser.pBeforeNoPadding".!!!(rBeforeNoPadding.r)

-

-    val rBeforeNoPaddingOrDelim = """(?s)(.*?)(?=(\z))"""

-    val pBeforeNoPaddingOrDelim: Parser[String] = "generateEscapeCharacterSameNoPadParser.pBeforeNoPaddingOrDelim".!!!(rBeforeNoPaddingOrDelim.r)

-

-    // Here because of the nature of using the same character for escape and escapeEscape

-    // we need to capture the escapes if they exist and make them part of the 'before'

-    val pBeforeAndEscsIgnoreTrailingPadding = "generateEscapeCharacterSameNoPadParser.pBeforeAndEscsIgnoreTrailingPadding".!!!(pBeforeIgnoreTrailingPadding ~! opt(pEscapes)) ^^ {

-      case (b ~ None) => b

-      case (b ~ Some(e)) => (b + e)

-    }

-    val pBeforeAndEscsNoPadding = "generateEscapeCharacterSameNoPadParser.pBeforeAndEscsNoPadding".!!!(pBeforeNoPadding ~! opt(pEscapes)) ^^ {

-      case (b ~ None) => b

-      case (b ~ Some(e)) => (b + e)

-    }

-

-    val pEscapedEscapeFollowedByDelim = "generateEscapeCharacterSameNoPadParser.pEscapedEscapeFollowedByDelim".!!!(pEscapedEscape ~! pDelims) ^^ { case (eses ~ d) => d }

-    val pDelimNotPrecededByEscape = "generateEscapeCharacterSameNoPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~ pDelims) ^^ { case (notEsc ~ d) => d } // Backtracking has to be allowed here

-    //val pUnescapedDelims = ((pEscapedEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | pDelims

-    val pUnescapedDelims = "generateEscapeCharacterSameNoPadParser.pUnescapedDelims".!!!(pEscapedEscapeFollowedByDelim | pDelimNotPrecededByEscape | pDelims)

-

-    val pFieldAndDelim: Parser[(Vector[String], String)] = (isMissingDelimAllowed, hasDelim) match {

-      case (false, true) => {

-        val contentDelimReq: Parser[(Vector[String], String)] = "generateEscapeCharacterSameNoPadParser.content_ReqDelims".!!!(pBeforeAndEscsIgnoreTrailingPadding ~! pUnescapedDelims) ^^ { case (b ~ d) => (Vector(b) -> d) }

-        contentDelimReq

-      }

-      case (true, true) => {

-        val content: Parser[(Vector[String], String)] = "generateEscapeCharacterSameNoPadParser.content_OptDelims".!!!((pBeforeAndEscsNoPadding) ~! (pUnescapedDelims | EOF)) ^^ { case (b ~ d) => (Vector(b) -> d) }

-        content

-      }

-      case (true, false) => {

-        val content: Parser[(Vector[String], String)] = "generateEscapeCharacterSameNoPadParser.content_NoDelims".!!!(pBeforeNoPaddingOrDelim ~! (EOF)) ^^ { case (b ~ d) => (Vector(b) -> d) }

-        content

-      }

-      case _ => Assert.invariantFailed() // return failedResult

-    }

-    pFieldAndDelim

-  }

-

-  protected def generateEscapeCharacterSameWithPadParser(separators: Parser[String], terminators: Parser[String],

-    escapeCharacterRegex: String, delimsRegex: String,

-    justification: TextJustificationType.Type,

-    padCharRegex: String, hasDelim: Boolean,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    val pSeps: Parser[String] = "generateEscapeCharacterSameWithPadParser.pSeps".!!!(separators)

-    val pTerms: Parser[String] = "generateEscapeCharacterSameWithPadParser.pTerms".!!!(terminators)

-    val pDelims: Parser[String] = "generateEscapeCharacterSameWithPadParser.pDelims".!!!(pSeps ||| pTerms)

-

-    val pEscape: Parser[String] = "generateEscapeCharacterSameWithPadParser.pEscape".!!!(escapeCharacterRegex.r)

-    val pEscapedEscape = "generateEscapeCharacterSameWithPadParser.pEscapedEscape".!!!(pEscape ~! pEscape) ^^ { case (e1 ~ e2) => (e1 + e2) } // concatenate escapes

-

-    // Parser captures and creates a string representation of the escapes

-    val pEscapes = "generateEscapeCharacterSameWithPadParser.pEscapes".!!!((pEscapedEscape.*) ~! opt(pEscape)) ^^ {

-      case (l ~ None) => l.mkString

-      case (l ~ Some(esc)) => l.mkString + esc

-    }

-

-    // Need to tolerate reading to end of data here

-    //

-    // Assumptions:

-    //	A field can be terminated by...

-    //		Unescaped delimiter

-    //		Padding followed by unescaped delimiter

-    //		Padding followed by end of data

-    //		End of data

-    //

-    //    val rBefore = """(?s)(.*?)(?=""" + // Give me everything from this point until...

-    //      """(?:(%1$s*)(%3$s*)(((?<!%1$s)(%2$s))|(\z)))""" + // An unescaped/escaped pad char followed by either an unescaped delimiter or end of data

-    //      """|""" + // OR

-    //      """(%1$s(%3$s+)(%2$s|\z))""" + // escape followed by one or more pad chars followed by a delimiter or end of data

-    //      """|""" + // OR

-    //      """((?<!%1$s)((%1$s%1$s)*)(%2$s))""" + // unescaped delimiter

-    //      """|""" + // OR

-    //      """\z)""" // End of data/file

-    //    val pBefore: Parser[String] = rBefore.format(escapeCharacterRegex, delimsRegex, padCharRegex).r

-

-    val rBefore = """(?s)(.*?)(?=""" + // Give me everything from this point until...

-      """(?:(""" + escapeCharacterRegex + """*)(""" + padCharRegex + """*)(((?<!""" +

-      escapeCharacterRegex + """)(""" + delimsRegex + """))|(\z)))""" + // An unescaped/escaped pad char followed by either an unescaped delimiter or end of data

-      """|""" + // OR

-      """(""" + escapeCharacterRegex + """(""" + padCharRegex + """+)(""" + delimsRegex + """|\z))""" + // escape followed by one or more pad chars followed by a delimiter or end of data

-      """|""" + // OR

-      """((?<!""" + escapeCharacterRegex + """)((""" + escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))""" + // unescaped delimiter

-      """|""" + // OR

-      """\z)""" // End of data/file

-    val pBefore: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBefore".!!!(rBefore.r)

-

-    //    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=((?<!%1$s)((%1$s%1$s)*)(%2$s))|(\z))"""

-    //    val pBeforeIgnoreTrailingPadding: Parser[String] = rBeforeIgnoreTrailingPadding.format(escapeCharacterRegex, delimsRegex).r

-    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=((?<!""" + escapeCharacterRegex + """)((""" +

-      escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))|(\z))"""

-    val pBeforeIgnoreTrailingPadding: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBeforeIgnoreTrailingPadding".!!!(rBeforeIgnoreTrailingPadding.r)

-

-    //    val rBeforeNoPadding = """(?s)(.*?)(?=(""" + // Give me everything from this point until...

-    //      """((?<!%1$s)((%1$s%1$s)*)(%2$s))""" + // unescaped delimiter

-    //      """|""" + // OR

-    //      """\z))""" // End of data/file

-    //    val pBeforeNoPadding: Parser[String] = rBeforeNoPadding.format(escapeCharacterRegex, delimsRegex).r

-

-    // TODO: This appears to be used in multiple places, pull out

-    val rBeforeNoPadding = """(?s)(.*?)(?=(""" + // Give me everything from this point until...

-      """((?<!""" + escapeCharacterRegex + """)((""" + escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))""" + // unescaped delimiter

-      """|""" + // OR

-      """\z))""" // End of data/file

-    val pBeforeNoPadding: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBeforeNoPadding".!!!(rBeforeNoPadding.r)

-

-    val rBeforeNoPaddingOrDelim = """(?s)(.*?)(?=(\z))"""

-    val pBeforeNoPaddingOrDelim: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBeforeNoPaddingOrDelim".!!!(rBeforeNoPaddingOrDelim.r)

-

-    //    val rBeforeNoDelim = """(?s)(.*?)(?=""" + // Give me everything from this point until...

-    //      """(?:(%1$s*)(%3$s*)(\z))""" + // An unescaped/escaped pad char followed by end of data

-    //      """|""" + // OR

-    //      """(%1$s(%3$s+)(\z))""" + // escape followed by one or more pad chars followed by a delimiter or end of data

-    //      """|""" + // OR

-    //      """\z)""" // End of data/file

-    //    val pBeforeNoDelim: Parser[String] = rBeforeNoDelim.format(escapeCharacterRegex, delimsRegex, padCharRegex).r

-

-    val rBeforeNoDelim = """(?s)(.*?)(?=""" + // Give me everything from this point until...

-      """(?:(""" + escapeCharacterRegex + """*)(""" + padCharRegex + """*)(\z))""" + // An unescaped/escaped pad char followed by end of data

-      """|""" + // OR

-      """(""" + escapeCharacterRegex + """(""" + padCharRegex + """+)(\z))""" + // escape followed by one or more pad chars followed by a delimiter or end of data

-      """|""" + // OR

-      """\z)""" // End of data/file

-    val pBeforeNoDelim: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBeforeNoDelim".!!!(rBeforeNoDelim.r)

-

-    // Here because of the nature of using the same character for escape and escapeEscape

-    // we need to capture the escapes if they exist and make them part of the 'before'

-    val pBeforeAndEscsIgnoreTrailingPadding = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscsIgnoreTrailingPadding".!!!(pBeforeIgnoreTrailingPadding ~! opt(pEscapes)) ^^ {

-      case (b ~ None) => b

-      case (b ~ Some(e)) => (b + e)

-    }

-

-    val pBeforeAndEscs = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscs".!!!(pBefore ~! opt(pEscapes | pEscapedEscape | pEscape)) ^^ {

-      case (b ~ None) => b

-      case (b ~ Some(e)) => (b + e)

-    }

-

-    val pEscapedEscapeFollowedByDelim = "generateEscapeCharacterSameWithPadParser.pEscapedEscapeFollwedByDelim".!!!(pEscapedEscape ~! pDelims) ^^ { case (eses ~ d) => d }

-    val pDelimNotPrecededByEscape = "generateEscapeCharacterSameWithPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~! pDelims) ^^ { case (notEsc ~ d) => d }

-    //val pUnescapedDelims = ((pEscapedEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | pDelims

-    val pUnescapedDelims = "generateEscapeCharacterSameWithPadParser.pUnescapedDelims".!!!(pEscapedEscapeFollowedByDelim | pDelimNotPrecededByEscape | pDelims)

-

-    //    val rPadChar = """(%s*)"""

-    //    val pPadChar: Parser[String] = rPadChar.format(padCharRegex).r

-    val rPadChar = """(""" + padCharRegex + """*)"""

-    val pPadChar: Parser[String] = "generateEscapeCharacterSameWithPadParser.pPadChar".!!!(rPadChar.r)

-    //    val rLeftPadChar = """(%1$s*)(?=([^%1$s]))""" // LeftPad precedes non pad characters

-    //    val pLeftPadChar: Parser[String] = rLeftPadChar.format(padCharRegex).r

-    val rLeftPadChar = """(""" + padCharRegex + """*)(?=([^""" + padCharRegex + """]))""" // LeftPad precedes non pad characters

-    val pLeftPadChar: Parser[String] = "generateEscapeCharacterSameWithPadParser.pLeftPadChar".!!!(rLeftPadChar.r)

-

-    val paddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.paddedContent".!!!(pLeftPadChar ~! pBeforeAndEscs ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }

-    val leftPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.leftPaddedContent".!!!(pLeftPadChar ~! pBeforeAndEscsIgnoreTrailingPadding) ^^ { case (lp ~ c) => Vector(lp, c) }

-    val rightPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.rightPaddedContent".!!!(pBeforeAndEscs ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }

-

-    //val hasDelim: Boolean = separators.size > 0 || terminators.size > 0

-

-    val pFieldAndDelim: Parser[(Vector[String], String)] = (justification, isMissingDelimAllowed, hasDelim) match {

-      case (TextJustificationType.Left, false, true) => {

-        val contentLeftDelimReq: Parser[(Vector[String], String)] = rightPaddedContent ~! pUnescapedDelims ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterSameWithPadParser.contentLeft_ReqDelims".!!!(contentLeftDelimReq)

-      }

-      case (TextJustificationType.Right, false, true) => {

-        val contentRightDelimReq: Parser[(Vector[String], String)] = leftPaddedContent ~! pUnescapedDelims ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterSameWithPadParser.contentRight_ReqDelims".!!!(contentRightDelimReq)

-      }

-      case (TextJustificationType.Center, false, true) => {

-        val contentCenterDelimReq: Parser[(Vector[String], String)] = paddedContent ~! pDelims ^^ { case (bc ~ d) => (bc -> d) }

-        "generateEscapeCharacterSameWithPadParser.contentCenter_ReqDelims".!!!(contentCenterDelimReq)

-      }

-      case (TextJustificationType.Left, true, true) => {

-        val contentLeft: Parser[(Vector[String], String)] = rightPaddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterSameWithPadParser.contentLeft_OptDelims".!!!(contentLeft)

-      }

-      case (TextJustificationType.Left, true, false) => {

-        val pBeforeAndEscs = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscs".!!!(pBeforeNoDelim ~! opt(pEscapes | pEscapedEscape | pEscape)) ^^ {

-          case (b ~ None) => b

-          case (b ~ Some(e)) => (b + e)

-        }

-        val rightPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.rightPaddedContent".!!!(pBeforeAndEscs ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }

-        val contentLeft: Parser[(Vector[String], String)] = rightPaddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterSameWithPadParser.contentLeft_NoDelims".!!!(contentLeft)

-      }

-      case (TextJustificationType.Right, true, true) => {

-        val contentRight: Parser[(Vector[String], String)] = leftPaddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterSameWithPadParser.contentRight_OptDelims".!!!(contentRight)

-      }

-      case (TextJustificationType.Right, true, false) => {

-        val pBeforeAndEscsIgnoreTrailingPadding = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscsIgnoreTrailingPadding".!!!(pBeforeNoDelim ~! opt(pEscapes)) ^^ {

-          case (b ~ None) => b

-          case (b ~ Some(e)) => (b + e)

-        }

-        val leftPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.leftPaddedContent".!!!(pLeftPadChar ~! pBeforeAndEscsIgnoreTrailingPadding) ^^ { case (lp ~ c) => Vector(lp, c) }

-        val contentRight: Parser[(Vector[String], String)] = leftPaddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterSameWithPadParser.contentRight_NoDelims".!!!(contentRight)

-      }

-      case (TextJustificationType.Center, true, true) => {

-        val contentCenter: Parser[(Vector[String], String)] = paddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterSameWithPadParser.contentCenter_OptDelims".!!!(contentCenter)

-      }

-      case (TextJustificationType.Center, true, false) => {

-        val pBeforeAndEscs = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscs".!!!(pBeforeNoDelim ~! opt(pEscapes | pEscapedEscape | pEscape)) ^^ {

-          case (b ~ None) => b

-          case (b ~ Some(e)) => (b + e)

-        }

-        val paddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.paddedContent".!!!(pLeftPadChar ~! pBeforeAndEscs ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }

-        val contentCenter: Parser[(Vector[String], String)] = paddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }

-        "generateEscapeCharacterSameWithPadParser.contentCenter_NoDelims".!!!(contentCenter)

-      }

-      case _ => Assert.invariantFailed() //return failedResult

-    }

-    pFieldAndDelim

-  }

-

-  def generateInputEscapeCharacterParser(separators: Set[String], terminators: Set[String],

-    escapeCharacter: String, escapeEscapeCharacter: String = "",

-    justification: TextJustificationType.Type,

-    padChar: String,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    Assert.invariant(escapeCharacter.length() != 0)

-

-    val hasEscEsc = escapeEscapeCharacter.length() > 0

-    val hasDelim = separators.size > 0 || terminators.size > 0

-    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)

-    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)

-    val padCharacterRegex = this.generateCharacterRegex(padChar)

-    val (pSeps, sepsRegex) = this.generateSeparators(separators)

-    val (pTerms, termsRegex) = this.generateTerminators(terminators)

-    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)

-

-    if (escapeEscapeCharacter.equals(escapeCharacter)) {

-      justification match {

-        case TextJustificationType.None => return this.generateEscapeCharacterSameNoPadParser(pSeps, pTerms,

-          escapeCharacterRegex, delimsRegex, hasDelim, isMissingDelimAllowed)

-        case _ => return this.generateEscapeCharacterSameWithPadParser(pSeps, pTerms,

-          escapeCharacterRegex, delimsRegex, justification, padChar, hasDelim, isMissingDelimAllowed)

-      }

-    } else {

-      justification match {

-        case TextJustificationType.None => return this.generateEscapeCharacterDiffNoPadParser(pSeps, pTerms,

-          escapeCharacterRegex, escapeEscapeCharacterRegex, delimsRegex, hasEscEsc, hasDelim,

-          isMissingDelimAllowed)

-        case _ => return this.generateEscapeCharacterDiffWithPadParser(pSeps, pTerms,

-          escapeCharacterRegex, escapeEscapeCharacterRegex, padCharacterRegex, delimsRegex, justification,

-          hasEscEsc, hasDelim, isMissingDelimAllowed)

-      }

-    }

-  }

-

-  def generateInputEscapeCharacterParser2(delims: Parser[String], delimsRegex: String,

-    hasDelim: Boolean,

-    escapeCharacter: String, escapeEscapeCharacter: String = "",

-    justification: TextJustificationType.Type,

-    padChar: String,

-    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {

-

-    Assert.invariant(escapeCharacter.length() != 0)

-

-    val hasEscEsc = escapeEscapeCharacter.length() > 0

-    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)

-    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)

-    val padCharacterRegex = this.generateCharacterRegex(padChar)

-

-    if (escapeEscapeCharacter.equals(escapeCharacter)) {

-      justification match {

-        case TextJustificationType.None => return this.generateEscapeCharacterSameNoPadParser(this.emptyParser, delims,

-          escapeCharacterRegex, delimsRegex, hasDelim, isMissingDelimAllowed)

-        case _ => return this.generateEscapeCharacterSameWithPadParser(this.emptyParser, delims,

-          escapeCharacterRegex, delimsRegex, justification, padChar, hasDelim, isMissingDelimAllowed)

-      }

-    } else {

-      justification match {

-        case TextJustificationType.None => return this.generateEscapeCharacterDiffNoPadParser(this.emptyParser, delims,

-          escapeCharacterRegex, escapeEscapeCharacterRegex, delimsRegex, hasEscEsc, hasDelim,

-          isMissingDelimAllowed)

-        case _ => return this.generateEscapeCharacterDiffWithPadParser(this.emptyParser, delims,

-          escapeCharacterRegex, escapeEscapeCharacterRegex, padCharacterRegex, delimsRegex, justification,

-          hasEscEsc, hasDelim, isMissingDelimAllowed)

-      }

-    }

-  }

-

-  def generateRemoveEscapeCharactersSameRegex(es: String): scala.util.matching.Regex = {

-    //val ERSplit = """(?s)(.*?)%1$s(.)(.*)""".format(es).r

-    val ERSplit = ("""(?s)(.*?)""" + es + """(.)(.*)""").r

-    ERSplit

-  }

-  def removeUnescapedEscapesRegex(eses: String, es: String): String = {

-    // ((?<!S)E)

-    //    val rRemoveUnescapedEscapes = """((?<!%1$s)%2$s)"""

-    //    val removeUnescapedEscapes = rRemoveUnescapedEscapes.format(eses, es)

-    val removeUnescapedEscapes = """((?<!""" + eses + """)""" + es + """)"""

-    removeUnescapedEscapes

-  }

-  def removeEscapeEscapesThatEscapeRegex(eses: String, es: String): String = {

-    // (S(?=E))

-    //    val rRemoveEscapeEscapesThatEscape = """(%1$s(?=%2$s))"""

-    //    val removeEscapeEscapesThatEscape = rRemoveEscapeEscapesThatEscape.format(eses, es)

-    val removeEscapeEscapesThatEscape = """(""" + eses + """(?=""" + es + """))"""

-    removeEscapeEscapesThatEscape

-  }

-  def removeEscapeRegex(es: String): String = {

-    //val rRemoveEscape = """(%1$s)""".format(es)

-    val rRemoveEscape = """(""" + es + """)"""

-    rRemoveEscape

-  }

-  def removeEscapesBlocksRegex(eses: String, endBlockRegex: String): String = {

-    // Replace escape at end of line OR replace escapeEscape preceding escapeBlockEnd

-    //    val rRemoveEscape = """(%1$s$)|(%1$s(?=%2$s))"""

-    //    val removeEscapes = rRemoveEscape.format(eses, endBlockRegex)

-    val removeEscapes = """(""" + eses + """$)|(""" + eses + """(?=""" + endBlockRegex + """))"""

-    removeEscapes

-  }

-

-  protected def parseInputDefaultContent(fieldParser: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],

-    input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {

-    //     withLoggingLevel(LogLevel.Debug)

-    {

-      val res = parseInputCommon(fieldParser, seps, terms, input,

-        "DelimParser.parseInputDefaultContent",

-        DelimiterLocation.Local) {

-          content =>

-            justification match {

-              case TextJustificationType.None => {

-                // content == Vector(content)

-                val field = content(0)

-                (field, content.mkString)

-              }

-              case TextJustificationType.Left => {

-                // content == Vector(content, padChars)

-                val field = content(0)

-                (field, content.mkString)

-              }

-              case TextJustificationType.Right => {

-                // content == Vector(padChars, content)

-                val field = content(1)

-                (field, content.mkString)

-              }

-              case TextJustificationType.Center => {

-                // content == Vector(padChars, content, padChars)

-                val field = content(1)

-                (field, content.mkString)

-              }

-            }

-        }

-      res

-    }

-  }

-

-  /**

-   * Notice this is curried. It takes a set of parameters, and then a body function

-   * which converts the results of a scala combinator parser (which our combinators

-   * use a Vector[String] for), and classifies it into two strings. One is the value region,

-   * the other the content region.

-   */

-  protected def parseInputCommon(

-    fieldParser: Parser[(Vector[String], String)],

-    seps: Parser[String],

-    terms: Parser[String],

-    input: Reader[Char],

-    logString: String,

-    dLoc: DelimiterLocation.Type)(

-      body: Vector[String] => (String, String)): DelimParseResult = {

-    //val pResult = this.parse(this.log(fieldParser)(logString), input)

-    val pResult = getLoggingLevel match {

-      case LogLevel.Debug => this.parse(this.log(fieldParser)(logString), input)

-      case _ => this.parse(fieldParser, input)

-    }

-

-    val result = pResult match {

-      case Success((blockedContent, theDelim), next) => {

-        val (theField, theParsedContent) = body(blockedContent)

-        val dResult = this.parse(seps, theDelim) // does our delimiter match the possible seps?

-        val dType =

-          if (dResult.isEmpty) DelimiterType.Terminator

-          else DelimiterType.Separator

-        DelimParseSuccessFactory(Success(theField, pResult.next), theDelim, dType, Some(theParsedContent),

-          dLoc)

-      }

-      case NoSuccess(msg, next) => {

-        DelimParseFailure(msg, next)

-      }

-    }

-    result

-  }

-

-  protected def parseInputEscapeBlockContent(

-    fieldParser: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],

-    input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {

-    // withLoggingLevel(LogLevel.Debug) 

-    val res = parseInputCommon(fieldParser, seps, terms, input, "DelimParser.parseInputEscapeBlockContent",

-      DelimiterLocation.Local) {

-        blockedContent =>

-          justification match {

-            case TextJustificationType.None => {

-              // blockedContent == Vector(blockStart, content, blockEnd)

-              val field = blockedContent(1)

-              (field, blockedContent.mkString)

-            }

-            case TextJustificationType.Left => {

-              // blockedContent == Vector(blockStart, content, padChars, blockEnd)

-              val field = blockedContent(1)

-              (field, blockedContent.mkString)

-            }

-            case TextJustificationType.Right => {

-              // blockedContent == Vector(blockStart, padChars, content, blockEnd)

-              val field = blockedContent(2)

-              (field, blockedContent.mkString)

-            }

-            case TextJustificationType.Center => {

-              // blockedContent == Vector(blockStart, padChars, content, padChars, blockEnd)

-              val field = blockedContent(2)

-              (field, blockedContent.mkString)

-            }

-          }

-      }

-    res

-  }

-

-  protected def parseInputEscapeCharContent(fieldParser: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],

-    input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {

-    parseInputDefaultContent(fieldParser, seps, terms, input, justification)

-  }

-}

+package edu.illinois.ncsa.daffodil.processors
+
+/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
+ *
+ * Developed by: Tresys Technology, LLC
+ *               http://www.tresys.com
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal with
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ * 
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimers.
+ * 
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimers in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *  3. Neither the names of Tresys Technology, nor the names of its contributors
+ *     may be used to endorse or promote products derived from this Software
+ *     without specific prior written permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
+ * SOFTWARE.
+ */
+
+import scala.util.parsing.combinator.RegexParsers
+import edu.illinois.ncsa.daffodil.util.Logging
+import scala.util.parsing.input.Reader
+import edu.illinois.ncsa.daffodil.util.Logging
+import edu.illinois.ncsa.daffodil.util.LogLevel
+import edu.illinois.ncsa.daffodil.util._
+import scala.collection.mutable.Queue
+import java.util.regex.Pattern
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import edu.illinois.ncsa.daffodil.processors.DelimiterType._
+import edu.illinois.ncsa.daffodil.processors.DelimiterLocation._
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+
+trait DebugRegexParsers extends RegexParsers with Logging {
+  /**
+   * Used to trace parsing
+   */
+  implicit def toLogged(name: String) = new {
+    def !!![T](p: Parser[T]) = log(p)(name)
+  }
+
+  /**
+   * A helper method that turns a `Parser` into one that will
+   *  print debugging information to stdout before and after
+   *  being applied.
+   */
+  override def log[T](p: => Parser[T])(name: String): Parser[T] = Parser { in =>
+    log(LogLevel.DelimDebug, "trying %s at %s", name, in)
+    val r = p(in)
+    log(LogLevel.DelimDebug, "end %s --> %s", name, r)
+    r
+  }
+
+}
+
+object TextJustificationType extends Enum {
+  sealed abstract trait Type extends EnumValueType
+  case object None extends Type
+  case object Left extends Type
+  case object Right extends Type
+  case object Center extends Type
+}
+
+sealed abstract class DelimParseResult(nextArg: Reader[Char]) {
+  def isSuccess: Boolean
+  def next = nextReader.asInstanceOf[DFDLCharReader]
+  def nextReader = nextArg
+}
+
+case class DelimParseSuccess(val delimiter: String,
+  val delimiterType: DelimiterType.Type,
+  val delimiterLoc: DelimiterLocation.Type,
+  val numBits: Int,
+  fieldArg: String,
+  nextArg: Reader[Char],
+  val numCharsRead: Int)
+  extends DelimParseResult(nextArg) {
+  def isSuccess = true
+  def field = fieldArg
+  def get = field
+}
+
+case class DelimParseFailure(msgArg: String, nextArg: Reader[Char])
+  extends DelimParseResult(nextArg) {
+  def isSuccess = false
+  def msg = msgArg
+}
+
+class DFDLDelimParserCommon(stringBitLengthFunction: String => Int) extends RegexParsers with DebugRegexParsers {
+  /**
+   * Thisobject has to be nested because it has as an argument type Success[String]
+   * and that type is only availble to things that implement the scala...Parsers trait.
+   *
+   * This is why you don't want to ball up all your stuff into a trait, you make reuse
+   * by derivation work, but you make reuse by encapsulation very difficult.
+   */
+  object DelimParseSuccessFactory {
+    /**
+     * If content is supplied then it is used to determine the field length.
+     * If None then the extracted field value itself is used.
+     */
+    def apply(res: Success[String], delimiter: String, delimiterType: DelimiterType.Type, contentOpt: Option[String],
+      dLoc: DelimiterLocation.Type) = {
+
+      val Success(fieldResult, next) = res
+      val content = contentOpt.getOrElse(res.get)
+      val charLength = content.length
+      val fieldResultBits = stringBitLengthFunction(content)
+      val result = new DelimParseSuccess(delimiter, delimiterType,
+        dLoc, fieldResultBits, fieldResult, next, charLength)
+      result
+    }
+  }
+
+  override val skipWhitespace = false
+
+  /**
+   * Need a parser that will always fail.
+   * Essentially a parser to reflect the ability to pass in an empty Separator
+   * or empty Terminator Set.
+   */
+  def parserAlwaysFail[T](expected: String)(name: String) = new Parser[T] {
+    def apply(in: Input): ParseResult[T] = {
+      Failure(name + " expected to fail.", in)
+    }
+  }
+
+  /**
+   * Constructs an Array of Parser[String] which holds the Parser representations
+   * of the delimList.
+   *
+   * Constructs an Array of String which holds the Regex representations of the
+   * delimList.
+   */
+  def buildDelims(delimList: Set[String]): (Array[Parser[String]], Array[String]) = {
+    var delimsParser: Queue[Parser[String]] = Queue.empty
+    var delimsRegex: Queue[String] = Queue.empty
+
+    // We probably always want delims ordered:
+    // Multi-char delims containing WSP+/*, WSP+, WSP*, multi-char delims, WSP, single-char delims
+
+    sortDelims(delimList).toList.foreach(str => {
+      val d = new Delimiter()
+      d(str)
+      delimsParser.enqueue(d.delimRegExParseDelim.r) // The regex representing the actual delimiter
+      delimsRegex.enqueue(d.delimRegExParseDelim) // The regex representing the actual delimiter
+    })
+    (delimsParser.toArray, delimsRegex.toArray)
+  }
+
+  /**
+   * Receives a Set of delimiters.  Returns a Set of (DelimRegexStr, OrigDelimStr)
+   */
+  def getDelimsRegex(delimList: Set[String]): Set[(String, String)] = {
+    var delims: Queue[(String, String)] = Queue.empty
+
+    delimList.foreach(str => {
+      val d = new Delimiter()
+      d(str)
+      delims.enqueue((d.delimRegExParseDelim, str))
+    })
+    delims.toSet
+  }
+
+  def sortDelims(delimList: Set[String]): Seq[String] = {
+    val wspStarByItself = delimList.filter(s => s == "%WSP*;")
+    val wspPlusByItself = delimList.filter(s => s == "%WSP+;")
+
+    val filteredDelimList = (delimList -- (wspStarByItself union wspPlusByItself))
+
+    val multiCharUnboundedLength = filteredDelimList.filter(s => (s.contains("%WSP*;") || s.contains("%WSP+;")))
+    val multiChar = (filteredDelimList -- multiCharUnboundedLength).filter(s => s.length() > 1)
+    val singleChar = filteredDelimList -- (multiChar union multiCharUnboundedLength)
+
+    val sortedUnbounded = multiCharUnboundedLength.toArray[String]
+    val sortedMultiChar = multiChar.toArray[String]
+
+    scala.util.Sorting.quickSort(sortedUnbounded)
+    scala.util.Sorting.quickSort(sortedMultiChar)
+
+    val orderedResultSeq: Seq[String] = sortedUnbounded.reverse.toSeq ++ wspPlusByItself ++ wspStarByItself ++ sortedMultiChar.reverse.toSeq ++ singleChar
+    orderedResultSeq
+  }
+
+  /**
+   * Combines the delimiters into a single alternation
+   */
+  def combineDelimitersRegex(sepsRegex: Array[String], termsRegex: Array[String]): String = {
+    val sb = new StringBuilder()
+    sepsRegex.foreach(x => {
+      sb.append(x)
+      sb.append("|")
+    })
+    termsRegex.foreach(x => {
+      sb.append(x)
+      sb.append("|")
+    })
+    val delimRegex = sb.toString().replaceFirst("[\\|]$", "") // trimEnd("|")
+    //    val seps = sepsRegex.mkString("|")
+    //    val terms = termsRegex.mkString("|")
+    //    val delimRegex = (seps + terms) //.replaceFirst("[\\|]$", "")
+    delimRegex
+  }
+
+  /**
+   * This function performs the "longest match" alternation by recursively combining
+   * all of the Parsers present in pArray.
+   *
+   * Assumes pArray is at least of length 1.
+   *
+   * pArray: An array of delimiters where each delimiter is represented by a Parser
+   * idx: The index of the current delimiter/Parser, defaults to 0
+   */
+  def combineLongest[T](pArray: Array[Parser[T]], idx: Int = 0): Parser[T] = {
+    val len = pArray.length
+
+    if (len == 0) { return parserAlwaysFail("")("empty delimiter list") }
+
+    val p0 = pArray(idx)
+
+    if ((idx + 1) == len) { p0 }
+    else {
+      (p0) ||| (combineLongest(pArray, idx + 1))
+    }
+  }
+
+  def convertDFDLLiteralToRegex(dfdlLiteral: String): String = {
+    var sb: StringBuilder = new StringBuilder("(")
+
+    dfdlLiteral foreach {
+      char =>
+        {
+          char match {
+            case '[' => sb.append("\\[")
+            case '\\' => sb.append("\\\\")
+            case '^' => sb.append("\\^")
+            case '$' => sb.append("\\$")
+            case '.' => sb.append("\\.")
+            case '|' => sb.append("\\|")
+            case '?' => sb.append("\\?")
+            case '*' => sb.append("\\*")
+            case '+' => sb.append("\\+")
+            case '(' => sb.append("\\(")
+            case ')' => sb.append("\\)")
+            case '{' => sb.append("\\{")
+            case '}' => sb.append("\\}")
+            case x => sb.append(x)
+          }
+        }
+    }
+    sb.append(")")
+    val res = sb.toString()
+    if (res.equals("()")) "" // get rid of empty group in regular expressions
+    else res
+  }
+
+  def getDfdlLiteralRegex(dfdlLiteralList: Set[String]): String = {
+    val (_, regex) = this.buildDelims(dfdlLiteralList)
+    combineDelimitersRegex(regex, Array.empty[String])
+  }
+
+  // TODO: does this handle %ES; or do we have to have outside separate checks for that?
+  // There is a separate check right now in LiteralNilDelimitedOrEndOfData. 
+  def isFieldDfdlLiteral(field: String, dfdlLiteralList: Set[String]): Boolean = {
+    val dfdlLiteralRegex = getDfdlLiteralRegex(dfdlLiteralList)
+    val m = Pattern.compile(dfdlLiteralRegex).matcher(field)
+    m.find()
+    m.matches()
+  }
+
+  lazy val EOF: Parser[String] = """\z""".r
+
+  def generateRemovePaddingParser(justification: TextJustificationType.Type, padChar: String): Option[Parser[String]] = {
+    if (padChar.length() == 0) return None
+    //val rAnything = """(.*?)(?=%s*$)""".format(padChar)
+    val rAnything = """(.*?)(?=""" + padChar + """*$)"""
+    val anything: Parser[String] = "generateRemovePaddingParser.anything".!!!(rAnything.r)
+    val padCharRegex = this.convertDFDLLiteralToRegex(padChar)
+    //val rPadCharLeft = """^(%s*)""".format(padCharRegex)
+    val rPadCharLeft = """^(""" + padCharRegex + """*)"""
+    val pPadCharLeft: Parser[String] = "generateRemovePaddingParser.pPadCharLeft".!!!(rPadCharLeft.r)
+    //val rPadCharRight = """(%s*)$""".format(padCharRegex)
+    val rPadCharRight = """(""" + padCharRegex + """*)$"""
+    val pPadCharRight: Parser[String] = "generateRemovePaddingParser.pPadCharRight".!!!(rPadCharRight.r)
+    val fieldCenter: Parser[String] = "generateRemovePaddingParser.fieldCenter".!!!(pPadCharLeft ~! anything ~! pPadCharRight) ^^ { case (l ~ a ~ r) => a }
+    val fieldLeft: Parser[String] = "generateRemovePaddingParser.fieldLeft".!!!(anything ~! pPadCharRight) ^^ { case (a ~ r) => a }
+    val fieldRight: Parser[String] = "generateRemovePaddingParser.fieldRight".!!!(pPadCharLeft ~! anything) ^^ { case (l ~ a) => a }
+    val result: Option[Parser[String]] = justification match {
+      case TextJustificationType.None => None
+      case TextJustificationType.Left => Some(fieldLeft)
+      case TextJustificationType.Right => Some(fieldRight)
+      case TextJustificationType.Center => Some(fieldCenter)
+    }
+    result
+  }
+
+  def generateInputPatternedParser(pattern: String): Parser[String] = {
+    val thePattern: Parser[String] = "generateInputPatternedParser.thePattern".!!!(("(?s)" + pattern).r)
+    //val entry = thePattern <~ opt(EOF)
+    //    val e0 = thePattern
+    //    val e1 = thePattern ~! EOF ^^ { case(p ~ e) => p}
+    //    val entry = e0 ||| e1
+    val entry = "generateInputPatternedParser.entry".!!!(thePattern ~! opt(EOF)) ^^ {
+      case (p ~ _) => p
+    }
+    entry
+  }
+
+  def generateInputNCharactersParser(nChars: Long): Parser[String] = {
+    val anything: Parser[String] = "generateInputNCharactersParser.anything".!!!(""".*""".r)
+    val rNChars = """(?s).{""" + nChars.toString() + """}"""
+    //val firstNChars: Parser[String] = String.format("""(?s).{%s}""", nChars.toString()).r
+    val firstNChars: Parser[String] = "generateInputNCharactersParser.firstNChars".!!!(rNChars.r)
+    val entry = firstNChars //<~ anything // Technically shouldn't need to add anything, we only want the first nChars
+    entry
+  }
+
+  def generateCombinedDelimsParser(localDelims: Set[String], remoteDelims: Set[String]): Parser[String] = {
+    val (localDelimsParser, localDelimsRegex) = this.buildDelims(localDelims)
+    val (remoteDelimsParser, remoteDelimsRegex) = this.buildDelims(remoteDelims)
+    val combinedDelims = remoteDelimsParser ++ localDelimsParser
+    val combinedDelimsParser = this.combineLongest(combinedDelims)
+    combinedDelimsParser
+  }
+
+  def generateCombinedDelimsParser(localDelimsParser: Array[Parser[String]],
+    remoteDelimsParser: Array[Parser[String]]): Parser[String] = {
+    val combinedDelims = remoteDelimsParser ++ localDelimsParser
+    val combinedDelimsParser = this.combineLongest(combinedDelims)
+    combinedDelimsParser
+  }
+
+  def generateLocalDelimsRegex(localDelimsRegex: Array[String]): String = {
+    val newLocalDelimsRegex = "(?s)^(" + combineDelimitersRegex(localDelimsRegex, Array.empty[String]) + ")$"
+    newLocalDelimsRegex
+  }
+
+  def generateIsLocalDelimParser(localDelimsRegex: Array[String]): Parser[String] = {
+    val isLocalDelimsParser: Parser[String] = "generateIsLocalDelimParser.isLocalDelimsParser".!!!(generateLocalDelimsRegex(localDelimsRegex).r)
+    isLocalDelimsParser
+  }
+
+  def generateDelimiter(delims: Set[String]): (Array[Parser[String]], Array[String]) = {
+    val (delimsParser, delimsRegex) = this.buildDelims(delims)
+    (delimsParser, delimsRegex)
+  }
+
+  def generateInputDelimiterParsers(localDelims: Set[String], remoteDelims: Set[String]): (Parser[String], Parser[String], Set[(String, String)]) = {
+    val (localDelimsParser, localDelimsRegex) = this.buildDelims(localDelims)
+    val (remoteDelimsParser, remoteDelimsRegex) = this.buildDelims(remoteDelims)
+    val combinedDelims = localDelimsParser ++ remoteDelimsParser
+    val combinedDelimsParser = this.combineLongest(combinedDelims)
+    val isLocalDelimParser = generateIsLocalDelimParser(localDelimsRegex)
+    //val inputDelimiterParser = combinedDelimsParser <~ opt(EOF) // Should yield longest match of all the delimiters
+    //    val p0 = combinedDelimsParser ~! EOF ^^ { case(p ~ e) => p}
+    //    val p1 = combinedDelimsParser
+    //    val inputDelimiterParser = p0 ||| p1
+    val inputDelimiterParser = combinedDelimsParser ~! opt(EOF) ^^ {
+      case (d ~ _) => d
+    }
+    val isRemoteRegex = combineDelimitersRegex(remoteDelimsRegex, Array.empty)
+    (inputDelimiterParser, isLocalDelimParser, getDelimsRegex(remoteDelims))
+  }
+
+  def generateInputDelimiterParser(localDelims: Set[String], remoteDelims: Set[String]): Parser[String] = {
+    val combinedDelimsParser = generateCombinedDelimsParser(localDelims, remoteDelims)
+    //val entry = combinedDelimsParser <~ opt(EOF) // Should yield longest match of all the delimiters
+    //    val p0 = combinedDelimsParser ~! EOF ^^ { case (p ~ e) => p}
+    //    val p1 = combinedDelimsParser
+    //    val entry = p0 ||| p1
+    //    entry
+    val inputDelimiterParser = combinedDelimsParser ~! opt(EOF) ^^ {
+      case (d ~ _) => d
+    }
+    inputDelimiterParser
+  }
+
+  def generateInputDelimiterParser(localDelims: Array[Parser[String]], remoteDelims: Array[Parser[String]]): Parser[String] = {
+    val combinedDelimsParser = generateCombinedDelimsParser(localDelims, remoteDelims)
+    //val entry = combinedDelimsParser <~ opt(EOF) // Should yield longest match of all the delimiters
+    //    val p0 = combinedDelimsParser ~! EOF ^^ { case (p ~ e) => p}
+    //    val p1 = combinedDelimsParser
+    //    val entry = p0 ||| p1
+    val inputDelimiterParser = combinedDelimsParser ~! opt(EOF) ^^ {
+      case (d ~ _) => d
+    }
+    inputDelimiterParser
+  }
+
+  def generateCharacterRegex(character: String): String = {
+    val characterRegex = convertDFDLLiteralToRegex(character)
+    characterRegex
+  }
+  protected def generateSeparators(separators: Set[String]): (Parser[String], Array[String]) = {
+    val (sepsParser, sepsRegex) = this.buildDelims(separators)
+    val pSeps: Parser[String] = this.combineLongest(sepsParser)
+    (pSeps, sepsRegex)
+  }
+  protected def generateTerminators(terminators: Set[String]): (Parser[String], Array[String]) = {
+    val (termsParser, termsRegex) = this.buildDelims(terminators)
+    val pTerms: Parser[String] = this.combineLongest(termsParser)
+    (pTerms, termsRegex)
+  }
+
+  protected def generateInputWithPadParser(separators: Parser[String], terminators: Parser[String],
+    delimsRegex: String,
+    padCharRegex: String,
+    justification: TextJustificationType.Type,
+    hasDelim: Boolean,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val pSeps: Parser[String] = "generateInputWithPadParser.pSeps".!!!(separators)
+    val pTerms: Parser[String] = "generateInputWithPadParser.pTerms".!!!(terminators)
+
+    //val rPadChar = """(%s*)""".format(padCharRegex)
+    val rPadChar = """(""" + padCharRegex + """*)"""
+    val pPadChar: Parser[String] = "generateInputWithPadParser.pPadChar".!!!(rPadChar.r)
+    //val rBefore = """(?s)(.*?)(?=(%1$s+(%2$s))|(%1$s+\z)|(%2$s)|(\z))""".format(padCharRegex, delimsRegex)
+    val rBefore = """(?s)(.*?)(?=(""" + padCharRegex + """+(""" + delimsRegex + """))|(""" + padCharRegex + """+\z)|(""" + delimsRegex + """)|(\z))"""
+    val pBefore: Parser[String] = "generateInputWithPadParser.pBefore".!!!(rBefore.r)
+    //val rBeforeNoDelims = """(?s)(.*?)(?=(%1$s+\z)|(\z))""".format(padCharRegex)
+    val rBeforeNoDelims = """(?s)(.*?)(?=(""" + padCharRegex + """+\z)|(\z))"""
+    val pBeforeNoDelims: Parser[String] = "generateInputWithPadParser.pBeforeNoDelims".!!!(rBeforeNoDelims.r)
+    //val rBeforeNoPadding = """(?s)((.*?)(?=(%1$s)|(\z)))|(.*)""".format(delimsRegex)
+    val rBeforeNoPadding = """(?s)((.*?)(?=(""" + delimsRegex + """)|(\z)))|(.*)"""
+    val pBeforeNoPadding: Parser[String] = "generateInputWithPadParser.pBeforeNoPadding".!!!(rBeforeNoPadding.r)
+    val rBeforeNoPaddingOrDelims = """(?s)(.*?)(?=(\z))"""
+    val pBeforeNoPaddingOrDelims: Parser[String] = "generateInputWithPadParser.pBeforeNoPaddingOrDelims".!!!(rBeforeNoPaddingOrDelims.r)
+    val pDelims: Parser[String] = "generateInputWithPadParser.pDelims".!!!(pSeps ||| pTerms)
+
+    val paddedContent = "generateInputWithPadParser.paddedContent".!!!(pPadChar ~! pBefore ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }
+    val leftPaddedContent = "generateInputWithPadParser.leftPaddedContent".!!!(pPadChar ~! pBeforeNoPadding) ^^ { case (lp ~ c) => Vector(lp, c) }
+    val rightPaddedContent = "generateInputWithPadParser.rightPaddedContent".!!!(pBefore ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }
+
+    val pFieldAndDelim: Parser[(Vector[String], String)] = (justification, isMissingDelimAllowed, hasDelim) match {
+      case (TextJustificationType.Left, false, true) => {
+        val contentLeftDelimReq = "generateInputWithPadParser.contentLeft_ReqDelims".!!!(rightPaddedContent ~! pDelims) ^^ { case (c ~ d) => (c, d) }
+        contentLeftDelimReq
+      }
+      case (TextJustificationType.Right, false, true) => {
+        val contentRightDelimReq = "generateInputWithPadParser.contentRight_ReqDelims".!!!(leftPaddedContent ~! pDelims) ^^ { case (c ~ d) => (c, d) }
+        contentRightDelimReq
+      }
+      case (TextJustificationType.Center, false, true) => {
+        val contentCenterDelimReq = "generateInputWithPadParser.contentCenter_ReqDelims".!!!(paddedContent ~! pDelims) ^^ { case (c ~ d) => (c, d) }
+        contentCenterDelimReq
+      }
+      case (TextJustificationType.Left, true, true) => {
+        val contentLeft = "generateInputWithPadParser.contentLeft_OptDelims".!!!(rightPaddedContent ~! (pDelims | EOF)) ^^ { case (c ~ d) => (c, d) }
+        contentLeft
+      }
+      case (TextJustificationType.Left, true, false) => {
+        val rightPaddedContent = "generateInputWithPadParser.rightPaddedContent_NoDelims".!!!(pBeforeNoDelims ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }
+        val contentLeft = "generateInputWithPadParser.contentLeft_NoDelims".!!!(rightPaddedContent ~! (EOF)) ^^ { case (c ~ d) => (c, d) }
+        contentLeft
+      }
+      case (TextJustificationType.Right, true, true) => {
+        val contentRight = "generateInputWithPadParser.contentRight_OptDelims".!!!(leftPaddedContent ~! (pDelims | EOF)) ^^ { case (c ~ d) => (c, d) }
+        contentRight
+      }
+      case (TextJustificationType.Right, true, false) => {
+        val leftPaddedContent = "generateInputWithPadParser.leftPaddedContent_NoDelims".!!!(pPadChar ~! pBeforeNoDelims) ^^ { case (lp ~ c) => Vector(lp, c) }
+        val contentRight = "generateInputWithPadParser.contentRight_NoDelims".!!!(leftPaddedContent ~! (EOF)) ^^ { case (c ~ d) => (c, d) }
+        contentRight
+      }
+      case (TextJustificationType.Center, true, true) => {
+        val contentCenter = "generateInputWithPadParser.contentCenter_OptDelims".!!!(paddedContent ~! (pDelims | EOF)) ^^ { case (c ~ d) => (c, d) }
+        contentCenter
+      }
+      case (TextJustificationType.Center, true, false) => {
+        val paddedContent = "generateInputWithPadParser.paddedContent_NoDelims".!!!(pPadChar ~! pBeforeNoDelims ~ pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }
+        val contentCenter = "generateInputWithPadParser.contentCenter_NoDelims".!!!(paddedContent ~! (EOF)) ^^ { case (c ~ d) => (c, d) }
+        contentCenter
+      }
+      case _ => Assert.invariantFailed("not one of the combinations.") // return failedResult
+    }
+    pFieldAndDelim
+  }
+
+  protected def generateInputNoPadParser(separators: Parser[String], terminators: Parser[String],
+    delimsRegex: String, hasDelim: Boolean,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val pSeps: Parser[String] = "generateInputNoPadParser.pSeps".!!!(separators)
+    val pTerms: Parser[String] = "generateInputNoPadParser.pTerms".!!!(terminators)
+
+    //val rBeforeNoPadding = """(?s)((.*?)(?=(%1$s)|(\z)))|(.*)"""
+    //val pBeforeNoPadding: Parser[String] = rBeforeNoPadding.format(delimsRegex).r
+    val rBeforeNoPadding = """(?s)((.*?)(?=(""" + delimsRegex + """)|(\z)))|(.*)"""
+    val pBeforeNoPadding: Parser[String] = "generateInputNoPadParser.pBeforeNoPadding".!!!(rBeforeNoPadding.r)
+
+    val rBeforeNoPaddingOrDelims = """(?s)(.*?)(?=(\z))"""
+    val pBeforeNoPaddingOrDelims: Parser[String] = "generateInputNoPadParser.pBeforeNoPaddingOrDelims".!!!(rBeforeNoPaddingOrDelims.r)
+    val pDelims: Parser[String] = "generateInputNoPadParser.pDelims".!!!(pSeps ||| pTerms)
+
+    val pFieldAndDelim: Parser[(Vector[String], String)] = (isMissingDelimAllowed, hasDelim) match {
+      case (false, true) => {
+        val contentDelimReq = "generateInputNoPadParser.content_ReqDelims".!!!(pBeforeNoPadding ~! pDelims) ^^ { case (c ~ d) => (Vector(c), d) }
+        contentDelimReq
+      }
+      case (true, true) => {
+        val content = "generateInputNoPadParser.content_OptDelims".!!!(pBeforeNoPadding ~! (pDelims | EOF)) ^^ { case (c ~ d) => (Vector(c), d) }
+        content
+      }
+      case (true, false) => {
+        val content = "generateInputNoPadParser.content_NoDelims".!!!(pBeforeNoPaddingOrDelims ~! (EOF)) ^^ { case (c ~ d) => (Vector(c), d) }
+        content
+      }
+      case _ => Assert.invariantFailed("impossible combination") // return failedResult
+    }
+    pFieldAndDelim
+  }
+
+  def generateInputParser(separators: Set[String], terminators: Set[String],
+    justification: TextJustificationType.Type,
+    padChar: String,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+    val hasDelim: Boolean = separators.size > 0 || terminators.size > 0
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)
+    val padCharRegex = generateCharacterRegex(padChar);
+
+    justification match {
+      case TextJustificationType.None => return generateInputNoPadParser(pSeps, pTerms,
+        delimsRegex, hasDelim, isMissingDelimAllowed)
+      case _ => return generateInputWithPadParser(pSeps, pTerms, delimsRegex, padCharRegex,
+        justification, hasDelim, isMissingDelimAllowed)
+    }
+  }
+
+  def generateInputParser2(pSeps: Parser[String], pTerms: Parser[String],
+    sepsRegex: Array[String], termsRegex: Array[String], hasDelim: Boolean,
+    justification: TextJustificationType.Type, padChar: String,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)
+    val padCharRegex = generateCharacterRegex(padChar);
+
+    justification match {
+      case TextJustificationType.None => return generateInputNoPadParser(pSeps, pTerms,
+        delimsRegex, hasDelim, isMissingDelimAllowed)
+      case _ => return generateInputWithPadParser(pSeps, pTerms, delimsRegex, padCharRegex,
+        justification, hasDelim, isMissingDelimAllowed)
+    }
+  }
+
+  protected def generateEscapeBlockWithPadParser(separators: Parser[String], terminators: Parser[String],
+    escapeBlockStartRegex: String, escapeBlockEndRegex: String,
+    escapeEscapeCharacterRegex: String,
+    padCharRegex: String,
+    justification: TextJustificationType.Type,
+    hasEscEsc: Boolean,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val pEscape: Parser[String] = "generateEscapeBlockWithPadParser.pEscape".!!!(escapeEscapeCharacterRegex.r)
+    val pBlockStart: Parser[String] = "generateEscapeBlockWithPadParser.pBlockStart".!!!(escapeBlockStartRegex.r)
+    val pBlockEnd: Parser[String] = "generateEscapeBlockWithPadParser.pBlockEnd".!!!(escapeBlockEndRegex.r)
+
+    val pSeps: Parser[String] = "generateEscapeBlockWithPadParser.pSeps".!!!(separators)
+    val pTerms: Parser[String] = "generateEscapeBlockWithPadParser.pTerms".!!!(terminators)
+    val pDelims: Parser[String] = "generateEscapeBlockWithPadParser.pDelims".!!!(pSeps ||| pTerms)
+
+    //    val rUnescapedBlockStart = if (hasEscEsc) """(?<!%1$s)%2$s""" else """%2$s"""
+    //    val pUnescapedBlockStart: Parser[String] = rUnescapedBlockStart.format(escapeEscapeCharacterRegex, escapeBlockStartRegex).r
+    val rUnescapedBlockStart = if (hasEscEsc) """(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeBlockStartRegex + """""" else escapeBlockStartRegex
+    val pUnescapedBlockStart: Parser[String] = "generateEscapeBlockWithPadParser.pUnescapedBlockStart".!!!(rUnescapedBlockStart.r)
+    //    val rUnescapedBlockEnd = if (hasEscEsc) """(?<!%1$s)%2$s""" else """%2$s"""
+    //    val pUnescapedBlockEnd: Parser[String] = rUnescapedBlockEnd.format(escapeEscapeCharacterRegex, escapeBlockEndRegex).r
+    val rUnescapedBlockEnd = if (hasEscEsc) """(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeBlockEndRegex + """""" else escapeBlockEndRegex
+    val pUnescapedBlockEnd: Parser[String] = "generateEscapeBlockWithPadParser.pUnescapedBlockEnd".!!!(rUnescapedBlockEnd.r)
+    val rBeforeUnescapedBlockEnd = """(?s)(.*?)(?=(""" + rUnescapedBlockEnd + """))"""
+    //    val pBeforeUnescapedBlockEnd: Parser[String] = rBeforeUnescapedBlockEnd.format(escapeEscapeCharacterRegex, escapeBlockEndRegex).r
+    val pBeforeUnescapedBlockEnd: Parser[String] = "generateEscapeBlockWithPadParser.pBeforeUnescapedBlockEnd".!!!(rBeforeUnescapedBlockEnd.r)
+    val blockedContent: Parser[Vector[String]] = "generateEscapeBlockWithPadParser.blockedContent".!!!(pUnescapedBlockStart ~! pBeforeUnescapedBlockEnd ~! pUnescapedBlockEnd) ^^ { case (bs ~ c ~ be) => Vector(bs, c, be) }
+
+    //    val rPadChar = """(%s*)"""
+    //    val pPadChar: Parser[String] = rPadChar.format(padCharRegex).r
+    val rPadChar = """(""" + padCharRegex + """*)"""
+    val pPadChar: Parser[String] = "generateEscapeBlockWithPadParser.pPadChar".!!!(rPadChar.r)
+    val paddedBlockedContent: Parser[Vector[String]] = "generateEscapeBlockWithPadParser.paddedBlockedContent".!!!(pPadChar ~! blockedContent ~! pPadChar) ^^ { case (lp ~ bc ~ rp) => Vector(lp) ++ bc ++ Vector(rp) }
+    val leftPaddedBlockedContent: Parser[Vector[String]] = "generateEscapeBlockWithPadParser.leftPaddedBlockedContent".!!!(pPadChar ~! blockedContent) ^^ { case (lp ~ bc) => Vector(lp) ++ bc }
+    val rightPaddedBlockedContent: Parser[Vector[String]] = "generateEscapeBlockWithPadParser.rightPaddedBlockedContent".!!!(blockedContent ~! pPadChar) ^^ { case (bc ~ rp) => bc ++ Vector(rp) }
+
+    val pFieldAndDelim: Parser[(Vector[String], String)] = (justification -> isMissingDelimAllowed) match {
+      case (TextJustificationType.Left, false) => {
+        val contentLeftDelimReq: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentLeft_ReqDelims".!!!(rightPaddedBlockedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }
+        contentLeftDelimReq
+      }
+      case (TextJustificationType.Right, false) => {
+        val contentRightDelimReq: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentRight_ReqDelims".!!!(leftPaddedBlockedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }
+        contentRightDelimReq
+      }
+      case (TextJustificationType.Center, false) => {
+        val contentCenterDelimReq: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentCenter_ReqDelims".!!!(paddedBlockedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }
+        contentCenterDelimReq
+      }
+      case (TextJustificationType.Left, true) => {
+        val contentLeft: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentLeft_OptDelims".!!!(rightPaddedBlockedContent ~! (pDelims | EOF)) ^^ { case (bc ~ d) => (bc -> d) }
+        contentLeft
+      }
+      case (TextJustificationType.Right, true) => {
+        val contentRight: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentRight_OptDelims".!!!(leftPaddedBlockedContent ~! (pDelims | EOF)) ^^ { case (bc ~ d) => (bc -> d) }
+        contentRight
+      }
+      case (TextJustificationType.Center, true) => {
+        val contentCenter: Parser[(Vector[String], String)] = "generateEscapeBlockWithPadParser.contentCenter_OptDelims".!!!(paddedBlockedContent ~! (pDelims | EOF)) ^^ { case (bc ~ d) => (bc -> d) }
+        contentCenter
+      }
+      case (TextJustificationType.None, _) => {
+        Assert.invariantFailed("shouldn't be None. We got to this match based on knowing it is not None")
+      }
+    }
+    pFieldAndDelim
+  }
+
+  protected def generateEscapeBlockNoPadParser(separators: Parser[String], terminators: Parser[String],
+    escapeBlockStartRegex: String, escapeBlockEndRegex: String,
+    escapeEscapeCharacterRegex: String = "",
+    hasEscEsc: Boolean,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val pEscape: Parser[String] = "generateEscapeBlockNoPadParser.pEscape".!!!(escapeEscapeCharacterRegex.r)
+    val pBlockStart: Parser[String] = "generateEscapeBlockNoPadParser.pBlockStart".!!!(escapeBlockStartRegex.r)
+    val pBlockEnd: Parser[String] = "generateEscapeBlockNoPadParser.pBlockEnd".!!!(escapeBlockEndRegex.r)
+
+    val pSeps: Parser[String] = "generateEscapeBlockNoPadParser.pSeps".!!!(separators)
+    val pTerms: Parser[String] = "generateEscapeBlockNoPadParser.pTerms".!!!(terminators)
+    val pDelims: Parser[String] = "generateEscapeBlockNoPadParser.pDelims".!!!(pSeps ||| pTerms)
+
+    //    val rBeforeUnescapedBlockEnd = """(?s)(.*?)(?=(""" + rUnescapedBlockEnd + """))"""
+    //    val pBeforeUnescapedBlockEnd: Parser[String] = rBeforeUnescapedBlockEnd.format(escapeEscapeCharacterRegex, escapeBlockEndRegex).r
+    //    val rUnescapedBlockStart = if (hasEscEsc) """(?<!%1$s)%2$s""" else """%2$s"""
+    //    val pUnescapedBlockStart: Parser[String] = rUnescapedBlockStart.format(escapeEscapeCharacterRegex, escapeBlockStartRegex).r
+    val rUnescapedBlockStart = if (hasEscEsc) """(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeBlockStartRegex else escapeBlockStartRegex
+    val pUnescapedBlockStart: Parser[String] = "generateEscapeBlockNoPadParser.pUnescapedBlockStart".!!!(rUnescapedBlockStart.r)
+
+    //    val rUnescapedBlockEnd = if (hasEscEsc) """(?<!%1$s)%2$s""" else """%2$s"""
+    //    val pUnescapedBlockEnd: Parser[String] = rUnescapedBlockEnd.format(escapeEscapeCharacterRegex, escapeBlockEndRegex).r
+    val rUnescapedBlockEnd = if (hasEscEsc) """(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeBlockEndRegex else escapeBlockEndRegex
+    val pUnescapedBlockEnd: Parser[String] = "generateEscapeBlockNoPadParser.pUnescapedBlockEnd".!!!(rUnescapedBlockEnd.r)
+    val rBeforeUnescapedBlockEnd = """(?s)(.*?)(?=(""" + rUnescapedBlockEnd + """))"""
+    val pBeforeUnescapedBlockEnd: Parser[String] = "generateEscapeBlockNoPadParser.pBeforeUnescapedBlockEnd".!!!(rBeforeUnescapedBlockEnd.r)
+    val blockedContent: Parser[Vector[String]] = "generateEscapeBlockNoPadParser.blockedContent".!!!(pUnescapedBlockStart ~! pBeforeUnescapedBlockEnd ~! pUnescapedBlockEnd) ^^ { case (bs ~ c ~ be) => Vector(bs, c, be) }
+
+    val pFieldAndDelim: Parser[(Vector[String], String)] = isMissingDelimAllowed match {
+      case false => {
+        val contentDelimReq: Parser[(Vector[String], String)] = "generateEscapeBlockNoPadParser.content_ReqDelims".!!!(blockedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }
+        contentDelimReq
+      }
+      case true => {
+        val content: Parser[(Vector[String], String)] = "generateEscapeBlockNoPadParser.content_OptDelims".!!!(blockedContent ~! (pDelims | EOF)) ^^ { case (bc ~ d) => (bc -> d) }
+        content
+      }
+    }
+    pFieldAndDelim
+  }
+
+  def generateEscapeBlockParser(separators: Set[String], terminators: Set[String],
+    escapeBlockStart: String, escapeBlockEnd: String,
+    escapeEscapeCharacter: String = "",
+    justification: TextJustificationType.Type,
+    padChar: String,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0
+    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)
+    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)
+    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)
+    val padCharRegex = generateCharacterRegex(padChar);
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+
+    justification match {
+      case TextJustificationType.None => return this.generateEscapeBlockNoPadParser(pSeps, pTerms,
+        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)
+      case _ => return this.generateEscapeBlockWithPadParser(pSeps, pTerms,
+        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)
+    }
+  }
+
+  def generateEscapeBlockParsers(separators: Set[String], terminators: Set[String],
+    escapeBlockStart: String, escapeBlockEnd: String,
+    escapeEscapeCharacter: String = "",
+    justification: TextJustificationType.Type,
+    padChar: String,
+    isMissingDelimAllowed: Boolean = true): (Parser[(Vector[String], String)], Parser[String], Parser[String], Array[String], Array[String]) = {
+
+    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0
+    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)
+    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)
+    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)
+    val padCharRegex = generateCharacterRegex(padChar);
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+
+    val escapeBlockParser = justification match {
+      case TextJustificationType.None => this.generateEscapeBlockNoPadParser(pSeps, pTerms,
+        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)
+      case _ => this.generateEscapeBlockWithPadParser(pSeps, pTerms,
+        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)
+    }
+    (escapeBlockParser, pSeps, pTerms, sepsRegex, termsRegex)
+  }
+
+  lazy val emptyParser = { parserAlwaysFail("")("empty delimiter list") }
+
+  def generateEscapeBlockParsers2(delims: Parser[String],
+    escapeBlockStart: String, escapeBlockEnd: String,
+    escapeEscapeCharacter: String = "",
+    justification: TextJustificationType.Type,
+    padChar: String,
+    isMissingDelimAllowed: Boolean = true): (Parser[(Vector[String], String)], String, String) = {
+
+    val hasEscEsc: Boolean = escapeEscapeCharacter.length() > 0
+    val escapeBlockStartRegex = this.generateCharacterRegex(escapeBlockStart)
+    val escapeBlockEndRegex = generateCharacterRegex(escapeBlockEnd)
+    val escapeEscapeRegex = generateCharacterRegex(escapeEscapeCharacter)
+    val padCharRegex = generateCharacterRegex(padChar);
+
+    val escapeBlockParser = justification match {
+      case TextJustificationType.None => this.generateEscapeBlockNoPadParser(emptyParser, delims,
+        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, hasEscEsc, isMissingDelimAllowed)
+      case _ => this.generateEscapeBlockWithPadParser(emptyParser, delims,
+        escapeBlockStartRegex, escapeBlockEndRegex, escapeEscapeRegex, padCharRegex, justification, hasEscEsc, isMissingDelimAllowed)
+    }
+    (escapeBlockParser, escapeBlockEndRegex, escapeEscapeRegex)
+  }
+
+  protected def generateEscapeCharacterDiffWithPadParser(separators: Parser[String], terminators: Parser[String],
+    escapeCharacterRegex: String, escapeEscapeCharacterRegex: String, padCharRegex: String,
+    delimsRegex: String,
+    justification: TextJustificationType.Type, hasEscEsc: Boolean, hasDelim: Boolean,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val pSeps: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pSeps".!!!(separators)
+    val pTerms: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pTerms".!!!(terminators)
+    val pDelims: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pDelims".!!!(pSeps ||| pTerms)
+
+    val pEscape: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pEscape".!!!(escapeCharacterRegex.r)
+    val pEscapeEscape: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pEscapeEscape".!!!(escapeEscapeCharacterRegex.r)
+    val pEscapedEscape = "generateEscapeCharacterDiffWithPadParser.pEscapedEscape".!!!(pEscapeEscape ~! pEscape)
+
+    val pEscapeEscapeFollowedByEscapeFollowedByDelim = "generateEscapeCharacterDiffWithPadParser.pEscapeEscapeFollowedByEscapeFollowedByDelim".!!!((pEscapeEscape ~! pEscape) ~! pDelims) ^^ { case (eses ~ es ~ d) => d }
+    val pDelimNotPrecededByEscape = "generateEscapeCharacterDiffWithPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~! pDelims) ^^ { case (notEsc ~ d) => d }
+    val pEscEscFollowedByDelim = "generateEscapeCharacterDiffWithPadParser.pEscEscFollowedByDelim".!!!(pEscapeEscape ~! pDelims) ^^ { case (eses ~ d) => d }
+    //val pUnescapedDelims = ((pEscapeEscape ~! pEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | (pEscapeEscape ~> pDelims) | pDelims
+    val pUnescapedDelims = "generateEscapeCharacterDiffWithPadParser.pUnescapedDelims".!!!(pEscapeEscapeFollowedByEscapeFollowedByDelim | pDelimNotPrecededByEscape | pEscEscFollowedByDelim | pDelims)
+
+    //val rPadChar = """(%s*)"""
+    val rPadChar = """(""" + padCharRegex + """*)"""
+    val pPadChar: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pPadChar".!!!(rPadChar.r)
+    //val rLeftPadChar = """(%1$s*)(?=([^%1$s]))""" // LeftPad precedes non pad characters
+    val rLeftPadChar = """(""" + padCharRegex + """*)(?=([^""" + padCharRegex + """]))""" // LeftPad precedes non pad characters
+    val pLeftPadChar: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pLeftPadChar".!!!(rLeftPadChar.format(padCharRegex).r)
+
+    // Content is anything until:
+    // 1. Padding ~ delimiter
+    // 2. unescaped delimiter
+    // 3. Optional Padding ~ EndOfData
+    //    val rBefore = """(?s)(.*?)(?=(%4$s+%3$s)|((?<!(?<!%1$s)%2$s)%3$s)|(%4$s*\z))"""
+    //    val rBeforeNoEscEsc = """(?s)(.*?)(?=(%3$s+%2$s)|((?<!%1$s)%2$s)|(%3$s*\z))"""
+    //    val pBefore: Parser[String] = {
+    //      if (hasEscEsc) rBefore.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex, padCharRegex).r
+    //      else rBeforeNoEscEsc.format(escapeCharacterRegex, delimsRegex, padCharRegex).r
+    //    }
+
+    val pBefore: Parser[String] = {
+      if (hasEscEsc) {
+        val rBefore = """(?s)(.*?)(?=(""" + padCharRegex + """+""" + delimsRegex + """)|((?<!(?<!""" +
+          escapeEscapeCharacterRegex + """)""" + escapeCharacterRegex + """)""" + delimsRegex + """)|(""" + padCharRegex + """*\z))"""
+        "generateEscapeCharacterDiffWithPadParser.pBeforeWithEscEsc".!!!(rBefore.r)
+      } else {
+        val rBeforeNoEscEsc = """(?s)(.*?)(?=(""" + padCharRegex + """+""" + delimsRegex + """)|((?<!""" +
+          escapeCharacterRegex + """)""" + delimsRegex + """)|(""" + padCharRegex + """*\z))"""
+        "generateEscapeCharacterDiffWithPadParser.BeforeNoEscEsc".!!!(rBeforeNoEscEsc.r)
+      }
+    }
+
+    //    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=(?:(?<!(?<!%1$s)%2$s)%3$s)|\z)"""
+    //    val rBeforeIgnoreTrailingPaddingNoEscEsc = """(?s)(.*?)(?=(?:(?<!%1$s)%2$s)|\z)"""
+    //    val pBeforeIgnoreTrailingPadding: Parser[String] = {
+    //      if (hasEscEsc) rBeforeIgnoreTrailingPadding.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex).r
+    //      else rBeforeIgnoreTrailingPaddingNoEscEsc.format(escapeCharacterRegex, delimsRegex).r
+    //    }
+
+    val pBeforeIgnoreTrailingPadding: Parser[String] = {
+      if (hasEscEsc) {
+        val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=(?:(?<!(?<!""" + escapeEscapeCharacterRegex + """)""" +
+          escapeCharacterRegex + """)""" + delimsRegex + """)|\z)"""
+        "generateEscapeCharacterDiffWithPadParser.BeforeIgnoreTrailingPadding".!!!(rBeforeIgnoreTrailingPadding.r)
+      } else {
+        val rBeforeIgnoreTrailingPaddingNoEscEsc = """(?s)(.*?)(?=(?:(?<!""" + escapeCharacterRegex + """)""" +
+          delimsRegex + """)|\z)"""
+        "generateEscapeCharacterDiffWithPadParser.BeforeIgnoreTrailingPaddingNoEscEsc".!!!(rBeforeIgnoreTrailingPaddingNoEscEsc.r)
+      }
+    }
+
+    //    val rBeforeNoPadding = """(?s)(.*?)(?=((?<!(?<!%1$s)%2$s)%3$s)|(\z))"""
+    //    val rBeforeNoPaddingNoEscEsc = """(?s)(.*?)(?=((?<!%1$s)%2$s)|(\z))"""
+    //    val pBeforeNoPadding: Parser[String] = {
+    //      if (hasEscEsc) rBeforeNoPadding.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex).r
+    //      else rBeforeNoPaddingNoEscEsc.format(escapeCharacterRegex, delimsRegex).r
+    //    }
+
+    val pBeforeNoPadding: Parser[String] = {
+      if (hasEscEsc) {
+        val rBeforeNoPadding = """(?s)(.*?)(?=((?<!(?<!""" + escapeEscapeCharacterRegex + """)""" +
+          escapeCharacterRegex + """)""" + delimsRegex + """)|(\z))"""
+        "generateEscapeCharacterDiffWithPadParser.BeforeNoPadding".!!!(rBeforeNoPadding.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex).r)
+      } else {
+        val rBeforeNoPaddingNoEscEsc = """(?s)(.*?)(?=((?<!""" + escapeCharacterRegex + """)""" +
+          delimsRegex + """)|(\z))"""
+        "generateEscapeCharacterDiffWithPadParser.BeforeNoPaddingNoEscEsc".!!!(rBeforeNoPaddingNoEscEsc.format(escapeCharacterRegex, delimsRegex).r)
+      }
+    }
+
+    val rBeforeNoPaddingOrDelims = """(?s)(.*?)(?=(\z))"""
+    val pBeforeNoPaddingOrDelims: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pBeforeNoPaddingOrDelims".!!!(rBeforeNoPaddingOrDelims.r)
+
+    //    val rBeforeNoDelims = """(?s)(.*?)(?=(%1$s*\z))"""
+    //    val pBeforeNoDelims: Parser[String] = rBeforeNoDelims.format(padCharRegex).r
+    val rBeforeNoDelims = """(?s)(.*?)(?=(""" + padCharRegex + """*\z))"""
+    val pBeforeNoDelims: Parser[String] = "generateEscapeCharacterDiffWithPadParser.pBeforeNoDelims".!!!(rBeforeNoDelims.r)
+
+    val paddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.paddedContent".!!!(pLeftPadChar ~! pBefore ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }
+    val leftPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.leftPaddedContent".!!!(pLeftPadChar ~! pBefore) ^^ { case (lp ~ c) => Vector(lp, c) }
+    val rightPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.rightPaddedContent".!!!(pBefore ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }
+
+    val pFieldAndDelim: Parser[(Vector[String], String)] = (justification, isMissingDelimAllowed, hasDelim) match {
+      case (TextJustificationType.Left, false, true) => {
+        val contentLeftDelimReq: Parser[(Vector[String], String)] = "generateEscapeCharacterDiffWithPadParser.contentLeft_ReqDelims".!!!(rightPaddedContent ~! pUnescapedDelims) ^^ { case (c ~ d) => (c, d) }
+        contentLeftDelimReq
+      }
+      case (TextJustificationType.Right, false, true) => {
+        val contentRightDelimReq: Parser[(Vector[String], String)] = "generateEscapeCharacterDiffWithPadParser.contentRight_ReqDelims".!!!(leftPaddedContent ~! pUnescapedDelims) ^^ { case (c ~ d) => (c, d) }
+        contentRightDelimReq
+      }
+      case (TextJustificationType.Center, false, true) => {
+        val contentCenterDelimReq: Parser[(Vector[String], String)] = "generateEscapeCharacterDiffWithPadParser.contentCenter_ReqDelims".!!!(paddedContent ~! pDelims) ^^ { case (bc ~ d) => (bc -> d) }
+        contentCenterDelimReq
+      }
+      case (TextJustificationType.Left, true, true) => {
+        val contentLeft: Parser[(Vector[String], String)] = rightPaddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterDiffWithPadParser.contentLeft_OptDelims".!!!(contentLeft)
+      }
+      case (TextJustificationType.Left, true, false) => {
+        val rightPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.rightPaddedContent_NoDelims".!!!(pBeforeNoDelims ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }
+        val contentLeft: Parser[(Vector[String], String)] = rightPaddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterDiffWithPadParser.contentLeft_NoDelims".!!!(contentLeft)
+      }
+      case (TextJustificationType.Right, true, true) => {
+        val contentRight: Parser[(Vector[String], String)] = leftPaddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterDiffWithPadParser.contentRight_OptDelims".!!!(contentRight)
+      }
+      case (TextJustificationType.Right, true, false) => {
+        val leftPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.leftPaddedContent_NoDelims".!!!(pLeftPadChar ~! pBeforeNoDelims) ^^ { case (lp ~ c) => Vector(lp, c) }
+        val contentRight: Parser[(Vector[String], String)] = leftPaddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterDiffWithPadParser.contentRight_NoDelims".!!!(contentRight)
+      }
+      case (TextJustificationType.Center, true, true) => {
+        val contentCenter: Parser[(Vector[String], String)] = paddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterDiffWithPadParser.contentCenter_OptDelims".!!!(contentCenter)
+      }
+      case (TextJustificationType.Center, true, false) => {
+        val paddedContent: Parser[Vector[String]] = "generateEscapeCharacterDiffWithPadParser.paddedContent_NoDelims".!!!(pLeftPadChar ~! pBeforeNoDelims ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }
+        val contentCenter: Parser[(Vector[String], String)] = paddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterDiffWithPadParser.contentCenter_NoDelims".!!!(contentCenter)
+      }
+      case _ => Assert.invariantFailed("illegal combination") //return failedResult
+    }
+    pFieldAndDelim
+  }
+
+  protected def generateEscapeCharacterDiffNoPadParser(separators: Parser[String], terminators: Parser[String],
+    escapeCharacterRegex: String, escapeEscapeCharacterRegex: String, delimsRegex: String,
+    hasEscEsc: Boolean, hasDelim: Boolean,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val pSeps: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pSeps".!!!(separators)
+    val pTerms: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pTerms".!!!(terminators)
+    val pDelims: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pDelims".!!!(pSeps ||| pTerms)
+
+    val pEscape: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pEscape".!!!(escapeCharacterRegex.r)
+    val pEscapeEscape: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pEscapeEscape".!!!(escapeEscapeCharacterRegex.r)
+    val pEscapedEscape = "generateEscapeCharacterDiffNoPadParser.pEscapedEscape".!!!(pEscapeEscape ~! pEscape)
+    //val pUnescapedDelims = ((pEscapeEscape ~ pEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | (pEscapeEscape ~> pDelims) | pDelims
+    //    val pUnescapedDelims = {
+    //      if (hasEscEsc) { ((pEscapeEscape ~! pEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | (pEscapeEscape ~> pDelims) | pDelims }
+    //      else { (not(pEscape) ~> pDelims) | pDelims }
+    //    }
+
+    // TODO: Same as generateEscapeCharacterDiffWithPadParser, look at generating this separately and passing in
+    //val pEscapeEscapeFollowedByEscapeFollowedByDelim = ((pEscapeEscape ~! pEscape) ~! pDelims) ^^ { case (eses ~ es ~ d) => d }
+    val pDelimNotPrecededByEscape = "generateEscapeCharacterDiffNoPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~! pDelims) ^^ { case (notEsc ~ d) => d }
+    //val pEscEscFollowedByDelim = (pEscapeEscape ~! pDelims) ^^ { case (eses ~ d) => d }
+    //val pUnescapedDelims = pEscapeEscapeFollowedByEscapeFollowedByDelim | pDelimNotPrecededByEscape | pEscEscFollowedByDelim | pDelims
+    val pUnescapedDelims = "generateEscapeCharacterDiffNoPadParser.pUnescapedDelims".!!! {
+      if (hasEscEsc) {
+        // Seems like this is a case where backtracking has to be allowed for pDelimNotPrecededByEscape
+        val pEscapeEscapeFollowedByEscapeFollowedByDelim = "generateEscapeCharacterDiffNoPadParser.pEscapeEscapeFollowedByEscapeFollowedByDelim".!!!((pEscapeEscape ~! pEscape) ~! pDelims) ^^ { case (eses ~ es ~ d) => d }
+        val pDelimNotPrecededByEscape = "generateEscapeCharacterDiffNoPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~ pDelims) ^^ { case (notEsc ~ d) => d }
+        val pEscEscFollowedByDelim = "generateEscapeCharacterDiffNoPadParser.pEscEscFollowedByDelim".!!!(pEscapeEscape ~! pDelims) ^^ { case (eses ~ d) => d }
+        pEscapeEscapeFollowedByEscapeFollowedByDelim | pDelimNotPrecededByEscape | pEscEscFollowedByDelim | pDelims
+      } else {
+        pDelimNotPrecededByEscape | pDelims
+      }
+    }
+
+    //    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=(?:(?<!(?<!%1$s)%2$s)%3$s)|\z)"""
+    //    val rBeforeIgnoreTrailingPaddingNoEscEsc = """(?s)(.*?)(?=(?:(?<!%1$s)%2$s)|\z)"""
+    //    val pBeforeIgnoreTrailingPadding: Parser[String] = {
+    //      if (hasEscEsc) rBeforeIgnoreTrailingPadding.format(escapeEscapeCharacterRegex, escapeCharacterRegex, delimsRegex).r
+    //      else rBeforeIgnoreTrailingPaddingNoEscEsc.format(escapeCharacterRegex, delimsRegex).r
+    //    }
+
+    val pBeforeIgnoreTrailingPadding: Parser[String] = {
+      if (hasEscEsc) {
+        val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=(?:(?<!(?<!""" + escapeEscapeCharacterRegex + """)""" +
+          escapeCharacterRegex + """)""" + delimsRegex + """)|\z)"""
+        "generateEscapeCharacterDiffNoPadParser.BeforeIgnoreTrailingPadding".!!!(rBeforeIgnoreTrailingPadding.r)
+      } else {
+        val rBeforeIgnoreTrailingPaddingNoEscEsc = """(?s)(.*?)(?=(?:(?<!""" + escapeCharacterRegex + """)""" +
+          delimsRegex + """)|\z)"""
+        "generateEscapeCharacterDiffNoPadParser.BeforeIgnoreTrailingPaddingNoEscEsc".!!!(rBeforeIgnoreTrailingPaddingNoEscEsc.r)
+      }
+    }
+
+    val pBeforeNoPadding: Parser[String] = {
+      if (hasEscEsc) {
+        val rBeforeNoPadding = """(?s)(.*?)(?=((?<!(?<!""" + escapeEscapeCharacterRegex + """)""" + escapeCharacterRegex + """)""" + delimsRegex + """)|(\z))"""
+        "generateEscapeCharacterDiffNoPadParser.BeforeNoPadding".!!!(rBeforeNoPadding.r)
+      } else {
+        val rBeforeNoPaddingNoEscEsc = """(?s)(.*?)(?=((?<!""" + escapeCharacterRegex + """)""" + delimsRegex + """)|(\z))"""
+        "generateEscapeCharacterDiffNoPadParser.BeforeNoPaddingNoEscEsc".!!!(rBeforeNoPaddingNoEscEsc.r)
+      }
+    }
+
+    val rBeforeNoPaddingOrDelims = """(?s)(.*?)(?=(\z))"""
+    val pBeforeNoPaddingOrDelims: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pBeforeNoPaddingOrDelims".!!!(rBeforeNoPaddingOrDelims.r)
+
+    val rBeforeNoDelims = """(?s)(.*?)(?=(\z))"""
+    val pBeforeNoDelims: Parser[String] = "generateEscapeCharacterDiffNoPadParser.pBeforeNoDelims".!!!(rBeforeNoDelims.r)
+
+    val pFieldAndDelim: Parser[(Vector[String], String)] = (isMissingDelimAllowed, hasDelim) match {
+      case (false, true) => {
+        val contentDelimReq: Parser[(Vector[String], String)] = pBeforeNoPadding ~! pUnescapedDelims ^^ { case (b ~ d) => (Vector(b) -> d) }
+        "generateEscapeCharacterDiffNoPadParser.content_ReqDelims".!!!(contentDelimReq)
+      }
+      case (true, true) => {
+        val content: Parser[(Vector[String], String)] = pBeforeIgnoreTrailingPadding ~! (pUnescapedDelims | EOF) ^^ { case (b ~ d) => (Vector(b) -> d) }
+        "generateEscapeCharacterDiffNoPadParser.content_OptDelims".!!!(content)
+      }
+      case (true, false) => {
+        val content: Parser[(Vector[String], String)] = pBeforeNoPaddingOrDelims ~! (EOF) ^^ { case (b ~ d) => (Vector(b) -> d) }
+        "generateEscapeCharacterDiffNoPadParser.content_NoDelims".!!!(content)
+      }
+      case _ => Assert.invariantFailed() // return failedResult
+    }
+    pFieldAndDelim
+  }
+
+  protected def generateEscapeCharacterSameNoPadParser(separators: Parser[String], terminators: Parser[String],
+    escapeCharacterRegex: String, delimsRegex: String, hasDelim: Boolean,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val pSeps: Parser[String] = "generateEscapeCharacterSameNoPadParser.pSeps".!!!(separators)
+    val pTerms: Parser[String] = "generateEscapeCharacterSameNoPadParser.pTerms".!!!(terminators)
+    val pDelims: Parser[String] = "generateEscapeCharacterSameNoPadParser.pDelims".!!!(pSeps ||| pTerms)
+
+    val pEscape: Parser[String] = "generateEscapeCharacterSameNoPadParser.pEscape".!!!(escapeCharacterRegex.r)
+    val pEscapedEscape = "generateEscapeCharacterSameNoPadParser.pEscapedEscape".!!!(pEscape ~! pEscape) ^^ { case (e1 ~ e2) => (e1 + e2) } // concatenate escapes
+
+    // Parser captures and creates a string representation of the escapes
+    val pEscapes = "generateEscapeCharacterSameNoPadParser.pEscapes".!!!((pEscapedEscape.*) ~! opt(pEscape)) ^^ {
+      case (l ~ None) => l.mkString
+      case (l ~ Some(esc)) => l.mkString + esc
+    }
+
+    //    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=((?<!%1$s)((%1$s%1$s)*)(%2$s))|(\z))"""
+    //    val pBeforeIgnoreTrailingPadding: Parser[String] = rBeforeIgnoreTrailingPadding.format(escapeCharacterRegex, delimsRegex).r
+    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=((?<!""" + escapeCharacterRegex + """)((""" + escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))|(\z))"""
+    val pBeforeIgnoreTrailingPadding: Parser[String] = "generateEscapeCharacterSameNoPadParser.pBeforeIgnoreTrailingPadding".!!!(rBeforeIgnoreTrailingPadding.r)
+
+    //    val rBeforeNoPadding = """(?s)(.*?)(?=(""" + // Give me everything from this point until...
+    //      """((?<!%1$s)((%1$s%1$s)*)(%2$s))""" + // unescaped delimiter
+    //      """|""" + // OR
+    //      """\z))""" // End of data/file
+    //    val pBeforeNoPadding: Parser[String] = rBeforeNoPadding.format(escapeCharacterRegex, delimsRegex).r
+    val rBeforeNoPadding = """(?s)(.*?)(?=(""" + // Give me everything from this point until...
+      """((?<!""" + escapeCharacterRegex + """)((""" +
+      escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))""" + // unescaped delimiter
+      """|""" + // OR
+      """\z))""" // End of data/file
+
+    val pBeforeNoPadding: Parser[String] = "generateEscapeCharacterSameNoPadParser.pBeforeNoPadding".!!!(rBeforeNoPadding.r)
+
+    val rBeforeNoPaddingOrDelim = """(?s)(.*?)(?=(\z))"""
+    val pBeforeNoPaddingOrDelim: Parser[String] = "generateEscapeCharacterSameNoPadParser.pBeforeNoPaddingOrDelim".!!!(rBeforeNoPaddingOrDelim.r)
+
+    // Here because of the nature of using the same character for escape and escapeEscape
+    // we need to capture the escapes if they exist and make them part of the 'before'
+    val pBeforeAndEscsIgnoreTrailingPadding = "generateEscapeCharacterSameNoPadParser.pBeforeAndEscsIgnoreTrailingPadding".!!!(pBeforeIgnoreTrailingPadding ~! opt(pEscapes)) ^^ {
+      case (b ~ None) => b
+      case (b ~ Some(e)) => (b + e)
+    }
+    val pBeforeAndEscsNoPadding = "generateEscapeCharacterSameNoPadParser.pBeforeAndEscsNoPadding".!!!(pBeforeNoPadding ~! opt(pEscapes)) ^^ {
+      case (b ~ None) => b
+      case (b ~ Some(e)) => (b + e)
+    }
+
+    val pEscapedEscapeFollowedByDelim = "generateEscapeCharacterSameNoPadParser.pEscapedEscapeFollowedByDelim".!!!(pEscapedEscape ~! pDelims) ^^ { case (eses ~ d) => d }
+    val pDelimNotPrecededByEscape = "generateEscapeCharacterSameNoPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~ pDelims) ^^ { case (notEsc ~ d) => d } // Backtracking has to be allowed here
+    //val pUnescapedDelims = ((pEscapedEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | pDelims
+    val pUnescapedDelims = "generateEscapeCharacterSameNoPadParser.pUnescapedDelims".!!!(pEscapedEscapeFollowedByDelim | pDelimNotPrecededByEscape | pDelims)
+
+    val pFieldAndDelim: Parser[(Vector[String], String)] = (isMissingDelimAllowed, hasDelim) match {
+      case (false, true) => {
+        val contentDelimReq: Parser[(Vector[String], String)] = "generateEscapeCharacterSameNoPadParser.content_ReqDelims".!!!(pBeforeAndEscsIgnoreTrailingPadding ~! pUnescapedDelims) ^^ { case (b ~ d) => (Vector(b) -> d) }
+        contentDelimReq
+      }
+      case (true, true) => {
+        val content: Parser[(Vector[String], String)] = "generateEscapeCharacterSameNoPadParser.content_OptDelims".!!!((pBeforeAndEscsNoPadding) ~! (pUnescapedDelims | EOF)) ^^ { case (b ~ d) => (Vector(b) -> d) }
+        content
+      }
+      case (true, false) => {
+        val content: Parser[(Vector[String], String)] = "generateEscapeCharacterSameNoPadParser.content_NoDelims".!!!(pBeforeNoPaddingOrDelim ~! (EOF)) ^^ { case (b ~ d) => (Vector(b) -> d) }
+        content
+      }
+      case _ => Assert.invariantFailed() // return failedResult
+    }
+    pFieldAndDelim
+  }
+
+  protected def generateEscapeCharacterSameWithPadParser(separators: Parser[String], terminators: Parser[String],
+    escapeCharacterRegex: String, delimsRegex: String,
+    justification: TextJustificationType.Type,
+    padCharRegex: String, hasDelim: Boolean,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    val pSeps: Parser[String] = "generateEscapeCharacterSameWithPadParser.pSeps".!!!(separators)
+    val pTerms: Parser[String] = "generateEscapeCharacterSameWithPadParser.pTerms".!!!(terminators)
+    val pDelims: Parser[String] = "generateEscapeCharacterSameWithPadParser.pDelims".!!!(pSeps ||| pTerms)
+
+    val pEscape: Parser[String] = "generateEscapeCharacterSameWithPadParser.pEscape".!!!(escapeCharacterRegex.r)
+    val pEscapedEscape = "generateEscapeCharacterSameWithPadParser.pEscapedEscape".!!!(pEscape ~! pEscape) ^^ { case (e1 ~ e2) => (e1 + e2) } // concatenate escapes
+
+    // Parser captures and creates a string representation of the escapes
+    val pEscapes = "generateEscapeCharacterSameWithPadParser.pEscapes".!!!((pEscapedEscape.*) ~! opt(pEscape)) ^^ {
+      case (l ~ None) => l.mkString
+      case (l ~ Some(esc)) => l.mkString + esc
+    }
+
+    // Need to tolerate reading to end of data here
+    //
+    // Assumptions:
+    //	A field can be terminated by...
+    //		Unescaped delimiter
+    //		Padding followed by unescaped delimiter
+    //		Padding followed by end of data
+    //		End of data
+    //
+    //    val rBefore = """(?s)(.*?)(?=""" + // Give me everything from this point until...
+    //      """(?:(%1$s*)(%3$s*)(((?<!%1$s)(%2$s))|(\z)))""" + // An unescaped/escaped pad char followed by either an unescaped delimiter or end of data
+    //      """|""" + // OR
+    //      """(%1$s(%3$s+)(%2$s|\z))""" + // escape followed by one or more pad chars followed by a delimiter or end of data
+    //      """|""" + // OR
+    //      """((?<!%1$s)((%1$s%1$s)*)(%2$s))""" + // unescaped delimiter
+    //      """|""" + // OR
+    //      """\z)""" // End of data/file
+    //    val pBefore: Parser[String] = rBefore.format(escapeCharacterRegex, delimsRegex, padCharRegex).r
+
+    val rBefore = """(?s)(.*?)(?=""" + // Give me everything from this point until...
+      """(?:(""" + escapeCharacterRegex + """*)(""" + padCharRegex + """*)(((?<!""" +
+      escapeCharacterRegex + """)(""" + delimsRegex + """))|(\z)))""" + // An unescaped/escaped pad char followed by either an unescaped delimiter or end of data
+      """|""" + // OR
+      """(""" + escapeCharacterRegex + """(""" + padCharRegex + """+)(""" + delimsRegex + """|\z))""" + // escape followed by one or more pad chars followed by a delimiter or end of data
+      """|""" + // OR
+      """((?<!""" + escapeCharacterRegex + """)((""" + escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))""" + // unescaped delimiter
+      """|""" + // OR
+      """\z)""" // End of data/file
+    val pBefore: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBefore".!!!(rBefore.r)
+
+    //    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=((?<!%1$s)((%1$s%1$s)*)(%2$s))|(\z))"""
+    //    val pBeforeIgnoreTrailingPadding: Parser[String] = rBeforeIgnoreTrailingPadding.format(escapeCharacterRegex, delimsRegex).r
+    val rBeforeIgnoreTrailingPadding = """(?s)(.*?)(?=((?<!""" + escapeCharacterRegex + """)((""" +
+      escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))|(\z))"""
+    val pBeforeIgnoreTrailingPadding: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBeforeIgnoreTrailingPadding".!!!(rBeforeIgnoreTrailingPadding.r)
+
+    //    val rBeforeNoPadding = """(?s)(.*?)(?=(""" + // Give me everything from this point until...
+    //      """((?<!%1$s)((%1$s%1$s)*)(%2$s))""" + // unescaped delimiter
+    //      """|""" + // OR
+    //      """\z))""" // End of data/file
+    //    val pBeforeNoPadding: Parser[String] = rBeforeNoPadding.format(escapeCharacterRegex, delimsRegex).r
+
+    // TODO: This appears to be used in multiple places, pull out
+    val rBeforeNoPadding = """(?s)(.*?)(?=(""" + // Give me everything from this point until...
+      """((?<!""" + escapeCharacterRegex + """)((""" + escapeCharacterRegex + escapeCharacterRegex + """)*)(""" + delimsRegex + """))""" + // unescaped delimiter
+      """|""" + // OR
+      """\z))""" // End of data/file
+    val pBeforeNoPadding: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBeforeNoPadding".!!!(rBeforeNoPadding.r)
+
+    val rBeforeNoPaddingOrDelim = """(?s)(.*?)(?=(\z))"""
+    val pBeforeNoPaddingOrDelim: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBeforeNoPaddingOrDelim".!!!(rBeforeNoPaddingOrDelim.r)
+
+    //    val rBeforeNoDelim = """(?s)(.*?)(?=""" + // Give me everything from this point until...
+    //      """(?:(%1$s*)(%3$s*)(\z))""" + // An unescaped/escaped pad char followed by end of data
+    //      """|""" + // OR
+    //      """(%1$s(%3$s+)(\z))""" + // escape followed by one or more pad chars followed by a delimiter or end of data
+    //      """|""" + // OR
+    //      """\z)""" // End of data/file
+    //    val pBeforeNoDelim: Parser[String] = rBeforeNoDelim.format(escapeCharacterRegex, delimsRegex, padCharRegex).r
+
+    val rBeforeNoDelim = """(?s)(.*?)(?=""" + // Give me everything from this point until...
+      """(?:(""" + escapeCharacterRegex + """*)(""" + padCharRegex + """*)(\z))""" + // An unescaped/escaped pad char followed by end of data
+      """|""" + // OR
+      """(""" + escapeCharacterRegex + """(""" + padCharRegex + """+)(\z))""" + // escape followed by one or more pad chars followed by a delimiter or end of data
+      """|""" + // OR
+      """\z)""" // End of data/file
+    val pBeforeNoDelim: Parser[String] = "generateEscapeCharacterSameWithPadParser.pBeforeNoDelim".!!!(rBeforeNoDelim.r)
+
+    // Here because of the nature of using the same character for escape and escapeEscape
+    // we need to capture the escapes if they exist and make them part of the 'before'
+    val pBeforeAndEscsIgnoreTrailingPadding = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscsIgnoreTrailingPadding".!!!(pBeforeIgnoreTrailingPadding ~! opt(pEscapes)) ^^ {
+      case (b ~ None) => b
+      case (b ~ Some(e)) => (b + e)
+    }
+
+    val pBeforeAndEscs = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscs".!!!(pBefore ~! opt(pEscapes | pEscapedEscape | pEscape)) ^^ {
+      case (b ~ None) => b
+      case (b ~ Some(e)) => (b + e)
+    }
+
+    val pEscapedEscapeFollowedByDelim = "generateEscapeCharacterSameWithPadParser.pEscapedEscapeFollwedByDelim".!!!(pEscapedEscape ~! pDelims) ^^ { case (eses ~ d) => d }
+    val pDelimNotPrecededByEscape = "generateEscapeCharacterSameWithPadParser.pDelimNotPrecededByEscape".!!!(not(pEscape) ~! pDelims) ^^ { case (notEsc ~ d) => d }
+    //val pUnescapedDelims = ((pEscapedEscape) ~> pDelims) | (not(pEscape) ~> pDelims) | pDelims
+    val pUnescapedDelims = "generateEscapeCharacterSameWithPadParser.pUnescapedDelims".!!!(pEscapedEscapeFollowedByDelim | pDelimNotPrecededByEscape | pDelims)
+
+    //    val rPadChar = """(%s*)"""
+    //    val pPadChar: Parser[String] = rPadChar.format(padCharRegex).r
+    val rPadChar = """(""" + padCharRegex + """*)"""
+    val pPadChar: Parser[String] = "generateEscapeCharacterSameWithPadParser.pPadChar".!!!(rPadChar.r)
+    //    val rLeftPadChar = """(%1$s*)(?=([^%1$s]))""" // LeftPad precedes non pad characters
+    //    val pLeftPadChar: Parser[String] = rLeftPadChar.format(padCharRegex).r
+    val rLeftPadChar = """(""" + padCharRegex + """*)(?=([^""" + padCharRegex + """]))""" // LeftPad precedes non pad characters
+    val pLeftPadChar: Parser[String] = "generateEscapeCharacterSameWithPadParser.pLeftPadChar".!!!(rLeftPadChar.r)
+
+    val paddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.paddedContent".!!!(pLeftPadChar ~! pBeforeAndEscs ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }
+    val leftPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.leftPaddedContent".!!!(pLeftPadChar ~! pBeforeAndEscsIgnoreTrailingPadding) ^^ { case (lp ~ c) => Vector(lp, c) }
+    val rightPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.rightPaddedContent".!!!(pBeforeAndEscs ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }
+
+    //val hasDelim: Boolean = separators.size > 0 || terminators.size > 0
+
+    val pFieldAndDelim: Parser[(Vector[String], String)] = (justification, isMissingDelimAllowed, hasDelim) match {
+      case (TextJustificationType.Left, false, true) => {
+        val contentLeftDelimReq: Parser[(Vector[String], String)] = rightPaddedContent ~! pUnescapedDelims ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterSameWithPadParser.contentLeft_ReqDelims".!!!(contentLeftDelimReq)
+      }
+      case (TextJustificationType.Right, false, true) => {
+        val contentRightDelimReq: Parser[(Vector[String], String)] = leftPaddedContent ~! pUnescapedDelims ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterSameWithPadParser.contentRight_ReqDelims".!!!(contentRightDelimReq)
+      }
+      case (TextJustificationType.Center, false, true) => {
+        val contentCenterDelimReq: Parser[(Vector[String], String)] = paddedContent ~! pDelims ^^ { case (bc ~ d) => (bc -> d) }
+        "generateEscapeCharacterSameWithPadParser.contentCenter_ReqDelims".!!!(contentCenterDelimReq)
+      }
+      case (TextJustificationType.Left, true, true) => {
+        val contentLeft: Parser[(Vector[String], String)] = rightPaddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterSameWithPadParser.contentLeft_OptDelims".!!!(contentLeft)
+      }
+      case (TextJustificationType.Left, true, false) => {
+        val pBeforeAndEscs = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscs".!!!(pBeforeNoDelim ~! opt(pEscapes | pEscapedEscape | pEscape)) ^^ {
+          case (b ~ None) => b
+          case (b ~ Some(e)) => (b + e)
+        }
+        val rightPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.rightPaddedContent".!!!(pBeforeAndEscs ~! pPadChar) ^^ { case (c ~ rp) => Vector(c, rp) }
+        val contentLeft: Parser[(Vector[String], String)] = rightPaddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterSameWithPadParser.contentLeft_NoDelims".!!!(contentLeft)
+      }
+      case (TextJustificationType.Right, true, true) => {
+        val contentRight: Parser[(Vector[String], String)] = leftPaddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterSameWithPadParser.contentRight_OptDelims".!!!(contentRight)
+      }
+      case (TextJustificationType.Right, true, false) => {
+        val pBeforeAndEscsIgnoreTrailingPadding = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscsIgnoreTrailingPadding".!!!(pBeforeNoDelim ~! opt(pEscapes)) ^^ {
+          case (b ~ None) => b
+          case (b ~ Some(e)) => (b + e)
+        }
+        val leftPaddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.leftPaddedContent".!!!(pLeftPadChar ~! pBeforeAndEscsIgnoreTrailingPadding) ^^ { case (lp ~ c) => Vector(lp, c) }
+        val contentRight: Parser[(Vector[String], String)] = leftPaddedContent ~! (EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterSameWithPadParser.contentRight_NoDelims".!!!(contentRight)
+      }
+      case (TextJustificationType.Center, true, true) => {
+        val contentCenter: Parser[(Vector[String], String)] = paddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterSameWithPadParser.contentCenter_OptDelims".!!!(contentCenter)
+      }
+      case (TextJustificationType.Center, true, false) => {
+        val pBeforeAndEscs = "generateEscapeCharacterSameWithPadParser.pBeforeAndEscs".!!!(pBeforeNoDelim ~! opt(pEscapes | pEscapedEscape | pEscape)) ^^ {
+          case (b ~ None) => b
+          case (b ~ Some(e)) => (b + e)
+        }
+        val paddedContent: Parser[Vector[String]] = "generateEscapeCharacterSameWithPadParser.paddedContent".!!!(pLeftPadChar ~! pBeforeAndEscs ~! pPadChar) ^^ { case (lp ~ c ~ rp) => Vector(lp, c, rp) }
+        val contentCenter: Parser[(Vector[String], String)] = paddedContent ~! (pUnescapedDelims | EOF) ^^ { case (c ~ d) => (c, d) }
+        "generateEscapeCharacterSameWithPadParser.contentCenter_NoDelims".!!!(contentCenter)
+      }
+      case _ => Assert.invariantFailed() //return failedResult
+    }
+    pFieldAndDelim
+  }
+
+  def generateInputEscapeCharacterParser(separators: Set[String], terminators: Set[String],
+    escapeCharacter: String, escapeEscapeCharacter: String = "",
+    justification: TextJustificationType.Type,
+    padChar: String,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    Assert.invariant(escapeCharacter.length() != 0)
+
+    val hasEscEsc = escapeEscapeCharacter.length() > 0
+    val hasDelim = separators.size > 0 || terminators.size > 0
+    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)
+    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)
+    val padCharacterRegex = this.generateCharacterRegex(padChar)
+    val (pSeps, sepsRegex) = this.generateSeparators(separators)
+    val (pTerms, termsRegex) = this.generateTerminators(terminators)
+    val delimsRegex = combineDelimitersRegex(sepsRegex, termsRegex)
+
+    if (escapeEscapeCharacter.equals(escapeCharacter)) {
+      justification match {
+        case TextJustificationType.None => return this.generateEscapeCharacterSameNoPadParser(pSeps, pTerms,
+          escapeCharacterRegex, delimsRegex, hasDelim, isMissingDelimAllowed)
+        case _ => return this.generateEscapeCharacterSameWithPadParser(pSeps, pTerms,
+          escapeCharacterRegex, delimsRegex, justification, padChar, hasDelim, isMissingDelimAllowed)
+      }
+    } else {
+      justification match {
+        case TextJustificationType.None => return this.generateEscapeCharacterDiffNoPadParser(pSeps, pTerms,
+          escapeCharacterRegex, escapeEscapeCharacterRegex, delimsRegex, hasEscEsc, hasDelim,
+          isMissingDelimAllowed)
+        case _ => return this.generateEscapeCharacterDiffWithPadParser(pSeps, pTerms,
+          escapeCharacterRegex, escapeEscapeCharacterRegex, padCharacterRegex, delimsRegex, justification,
+          hasEscEsc, hasDelim, isMissingDelimAllowed)
+      }
+    }
+  }
+
+  def generateInputEscapeCharacterParser2(delims: Parser[String], delimsRegex: String,
+    hasDelim: Boolean,
+    escapeCharacter: String, escapeEscapeCharacter: String = "",
+    justification: TextJustificationType.Type,
+    padChar: String,
+    isMissingDelimAllowed: Boolean = true): Parser[(Vector[String], String)] = {
+
+    Assert.invariant(escapeCharacter.length() != 0)
+
+    val hasEscEsc = escapeEscapeCharacter.length() > 0
+    val escapeCharacterRegex = this.generateCharacterRegex(escapeCharacter)
+    val escapeEscapeCharacterRegex = this.generateCharacterRegex(escapeEscapeCharacter)
+    val padCharacterRegex = this.generateCharacterRegex(padChar)
+
+    if (escapeEscapeCharacter.equals(escapeCharacter)) {
+      justification match {
+        case TextJustificationType.None => return this.generateEscapeCharacterSameNoPadParser(this.emptyParser, delims,
+          escapeCharacterRegex, delimsRegex, hasDelim, isMissingDelimAllowed)
+        case _ => return this.generateEscapeCharacterSameWithPadParser(this.emptyParser, delims,
+          escapeCharacterRegex, delimsRegex, justification, padChar, hasDelim, isMissingDelimAllowed)
+      }
+    } else {
+      justification match {
+        case TextJustificationType.None => return this.generateEscapeCharacterDiffNoPadParser(this.emptyParser, delims,
+          escapeCharacterRegex, escapeEscapeCharacterRegex, delimsRegex, hasEscEsc, hasDelim,
+          isMissingDelimAllowed)
+        case _ => return this.generateEscapeCharacterDiffWithPadParser(this.emptyParser, delims,
+          escapeCharacterRegex, escapeEscapeCharacterRegex, padCharacterRegex, delimsRegex, justification,
+          hasEscEsc, hasDelim, isMissingDelimAllowed)
+      }
+    }
+  }
+
+  def generateRemoveEscapeCharactersSameRegex(es: String): scala.util.matching.Regex = {
+    //val ERSplit = """(?s)(.*?)%1$s(.)(.*)""".format(es).r
+    val ERSplit = ("""(?s)(.*?)""" + es + """(.)(.*)""").r
+    ERSplit
+  }
+  def removeUnescapedEscapesRegex(eses: String, es: String): String = {
+    // ((?<!S)E)
+    //    val rRemoveUnescapedEscapes = """((?<!%1$s)%2$s)"""
+    //    val removeUnescapedEscapes = rRemoveUnescapedEscapes.format(eses, es)
+    val removeUnescapedEscapes = """((?<!""" + eses + """)""" + es + """)"""
+    removeUnescapedEscapes
+  }
+  def removeEscapeEscapesThatEscapeRegex(eses: String, es: String): String = {
+    // (S(?=E))
+    //    val rRemoveEscapeEscapesThatEscape = """(%1$s(?=%2$s))"""
+    //    val removeEscapeEscapesThatEscape = rRemoveEscapeEscapesThatEscape.format(eses, es)
+    val removeEscapeEscapesThatEscape = """(""" + eses + """(?=""" + es + """))"""
+    removeEscapeEscapesThatEscape
+  }
+  def removeEscapeRegex(es: String): String = {
+    //val rRemoveEscape = """(%1$s)""".format(es)
+    val rRemoveEscape = """(""" + es + """)"""
+    rRemoveEscape
+  }
+  def removeEscapesBlocksRegex(eses: String, endBlockRegex: String): String = {
+    // Replace escape at end of line OR replace escapeEscape preceding escapeBlockEnd
+    //    val rRemoveEscape = """(%1$s$)|(%1$s(?=%2$s))"""
+    //    val removeEscapes = rRemoveEscape.format(eses, endBlockRegex)
+    val removeEscapes = """(""" + eses + """$)|(""" + eses + """(?=""" + endBlockRegex + """))"""
+    removeEscapes
+  }
+
+  protected def parseInputDefaultContent(fieldParser: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],
+    input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {
+    //     withLoggingLevel(LogLevel.Debug)
+    {
+      val res = parseInputCommon(fieldParser, seps, terms, input,
+        "DelimParser.parseInputDefaultContent",
+        DelimiterLocation.Local) {
+          content =>
+            justification match {
+              case TextJustificationType.None => {
+                // content == Vector(content)
+                val field = content(0)
+                (field, content.mkString)
+              }
+              case TextJustificationType.Left => {
+                // content == Vector(content, padChars)
+                val field = content(0)
+                (field, content.mkString)
+              }
+              case TextJustificationType.Right => {
+                // content == Vector(padChars, content)
+                val field = content(1)
+                (field, content.mkString)
+              }
+              case TextJustificationType.Center => {
+                // content == Vector(padChars, content, padChars)
+                val field = content(1)
+                (field, content.mkString)
+              }
+            }
+        }
+      res
+    }
+  }
+
+  /**
+   * Notice this is curried. It takes a set of parameters, and then a body function
+   * which converts the results of a scala combinator parser (which our combinators
+   * use a Vector[String] for), and classifies it into two strings. One is the value region,
+   * the other the content region.
+   */
+  protected def parseInputCommon(
+    fieldParser: Parser[(Vector[String], String)],
+    seps: Parser[String],
+    terms: Parser[String],
+    input: Reader[Char],
+    logString: String,
+    dLoc: DelimiterLocation.Type)(
+      body: Vector[String] => (String, String)): DelimParseResult = {
+    //val pResult = this.parse(this.log(fieldParser)(logString), input)
+    val pResult = getLoggingLevel match {
+      case LogLevel.Debug => this.parse(this.log(fieldParser)(logString), input)
+      case _ => this.parse(fieldParser, input)
+    }
+
+    val result = pResult match {
+      case Success((blockedContent, theDelim), next) => {
+        val (theField, theParsedContent) = body(blockedContent)
+        val dResult = this.parse(seps, theDelim) // does our delimiter match the possible seps?
+        val dType =
+          if (dResult.isEmpty) DelimiterType.Terminator
+          else DelimiterType.Separator
+        DelimParseSuccessFactory(Success(theField, pResult.next), theDelim, dType, Some(theParsedContent),
+          dLoc)
+      }
+      case NoSuccess(msg, next) => {
+        DelimParseFailure(msg, next)
+      }
+    }
+    result
+  }
+
+  protected def parseInputEscapeBlockContent(
+    fieldParser: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],
+    input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {
+    // withLoggingLevel(LogLevel.Debug) 
+    val res = parseInputCommon(fieldParser, seps, terms, input, "DelimParser.parseInputEscapeBlockContent",
+      DelimiterLocation.Local) {
+        blockedContent =>
+          justification match {
+            case TextJustificationType.None => {
+              // blockedContent == Vector(blockStart, content, blockEnd)
+              val field = blockedContent(1)
+              (field, blockedContent.mkString)
+            }
+            case TextJustificationType.Left => {
+              // blockedContent == Vector(blockStart, content, padChars, blockEnd)
+              val field = blockedContent(1)
+              (field, blockedContent.mkString)
+            }
+            case TextJustificationType.Right => {
+              // blockedContent == Vector(blockStart, padChars, content, blockEnd)
+              val field = blockedContent(2)
+              (field, blockedContent.mkString)
+            }
+            case TextJustificationType.Center => {
+              // blockedContent == Vector(blockStart, padChars, content, padChars, blockEnd)
+              val field = blockedContent(2)
+              (field, blockedContent.mkString)
+            }
+          }
+      }
+    res
+  }
+
+  protected def parseInputEscapeCharContent(fieldParser: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],
+    input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {
+    parseInputDefaultContent(fieldParser, seps, terms, input, justification)
+  }
+}
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParserStatic.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParserStatic.scala
index 1d4915f..0980f26 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParserStatic.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimParserStatic.scala
@@ -1,210 +1,210 @@
-package edu.illinois.ncsa.daffodil.processors

-

-/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.

- *

- * Developed by: Tresys Technology, LLC

- *               http://www.tresys.com

- * 

- * Permission is hereby granted, free of charge, to any person obtaining a copy of

- * this software and associated documentation files (the "Software"), to deal with

- * the Software without restriction, including without limitation the rights to

- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies

- * of the Software, and to permit persons to whom the Software is furnished to do

- * so, subject to the following conditions:

- * 

- *  1. Redistributions of source code must retain the above copyright notice,

- *     this list of conditions and the following disclaimers.

- * 

- *  2. Redistributions in binary form must reproduce the above copyright

- *     notice, this list of conditions and the following disclaimers in the

- *     documentation and/or other materials provided with the distribution.

- * 

- *  3. Neither the names of Tresys Technology, nor the names of its contributors

- *     may be used to endorse or promote products derived from this Software

- *     without specific prior written permission.

- * 

- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

- * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE

- * SOFTWARE.

- */

-

-import scala.util.parsing.combinator.RegexParsers

-import scala.util.parsing.input.Reader

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-

-class DFDLDelimParserStatic(stringBitLengthFunction: String => Int)

-  extends DFDLDelimParserCommon(stringBitLengthFunction) {

-

-  def parseInput(field: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],

-                 input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {

-    val result = this.parseInputDefaultContent(field, seps, terms, input, justification)

-    result

-  }

-

-  def parseInputDelimiter(inputDelimiterParser: Parser[String], isLocalDelimParser: Parser[String],

-                          input: Reader[Char]): DelimParseResult = {

-    val res = this.parse(this.log(inputDelimiterParser)("DelimParser.parseInputDelimiter.allDelims"), input)

-

-    // TODO: This seems pretty inefficient. We're redoing a match in order to know 

-    // whether it was local or remote?? 

-    val result = res match {

-      case s @ Success(delimiterResult, next) => {

-        // We have a result but was it a remote or local match?

-

-        val subResult = this.parseAll(this.log(isLocalDelimParser)("DelimParser.parseInputDelimiter.isLocal"), delimiterResult)

-        val delimiterLocation = if (subResult.isEmpty) DelimiterLocation.Remote else DelimiterLocation.Local

-        //

-        // TODO: ?? Is None the right thing to pass here?? If we pass none, then it is 

-        // going to determine the length based on the delimiterResult. Does that include

-        // everything it needs to include?

-        //

-        DelimParseSuccessFactory(s, delimiterResult, DelimiterType.NotDelimited, None, // Is None right?

-          delimiterLocation)

-      }

-      case NoSuccess(msg, next) => DelimParseFailure(msg, next)

-    }

-    result

-  }

-

-  def parseInputEscapeBlock(escapeBlockParser: Parser[(Vector[String], String)], seps: Parser[String],

-                            terms: Parser[String], input: Reader[Char], justification: TextJustificationType.Type,

-                            removeEscapeBlocksRegex: String, parseInputParser: Parser[(Vector[String], String)]): DelimParseResult = {

-    val result1 = this.parseInputEscapeBlockContent(escapeBlockParser, seps, terms, input, justification)

-    val result2 = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapeBlocks(field, removeEscapeBlocksRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-      }

-      case f: DelimParseFailure => this.parseInputDefaultContent(parseInputParser, seps, terms,

-        input, justification)

-    }

-    result2

-  }

-

-  private def removeEscapeBlocks(input: String, removeEscapeBlockRegex: String): String = {

-    input.replaceAll(removeEscapeBlockRegex, "")

-  }

-

-  def parseInputEscapeCharacter(escapeCharacterParser: Parser[(Vector[String], String)],

-                                seps: Parser[String], terms: Parser[String], input: Reader[Char],

-                                justification: TextJustificationType.Type,

-                                removeEscapeCharacterRegex: scala.util.matching.Regex,

-                                removeUnescapedEscapesRegex: String,

-                                removeEscapeEscapesThatEscapeRegex: String,

-                                removeEscapeRegex: String, es: String, eses: String): DelimParseResult = {

-    val result1 = this.parseInputEscapeCharContent(escapeCharacterParser, seps, terms, input, justification)

-    val result2 = result1 match {

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val newField = removeEscapeCharacters(field, eses, es, removeEscapeCharacterRegex, removeUnescapedEscapesRegex,

-          removeEscapeEscapesThatEscapeRegex, removeEscapeRegex)

-        s.copy(fieldArg = newField, nextArg = s.next)

-      }

-      case f: DelimParseFailure => f

-    }

-    result2

-  }

-

-  /**

-   * Assumes 'input' has had its delimiter picked off end already if it existed.

-   */

-  private def removeEscapeCharacters(input: String, eses: String, es: String,

-                                     removeEscCharsSameRegex: scala.util.matching.Regex,

-                                     removeUnescapedEscapesRegex: String,

-                                     removeEscapeEscapesThatEscapeRegex: String,

-                                     removeEscapeRegex: String): String = {

-    if (eses.equals(es)) {

-      return removeEscapeCharactersSame(input, removeEscCharsSameRegex)

-    } else {

-      return removeEscapeCharactersDiff(input, eses, removeUnescapedEscapesRegex,

-        removeEscapeEscapesThatEscapeRegex, removeEscapeRegex)

-    }

-  }

-

-  private def removeEscapeCharactersSame(input: String,

-                                         removeEscCharsSameRegex: scala.util.matching.Regex): String = {

-    // used to cleanup escape characters 

-    val ERSplit = removeEscCharsSameRegex

-    def removeActiveEscapes(str: String): String = {

-      val res = str match {

-        case ERSplit(before, theEsc, delim, after) => {

-          val rest = removeActiveEscapes(after)

-          before + delim + rest

-        }

-        case ERSplit(before, delim, after) => {

-          val rest = removeActiveEscapes(after)

-          before + delim + rest

-        }

-        case _ => str

-      }

-      res

-    }

-    removeActiveEscapes(input)

-  }

-

-  private def removeEscapeCharactersDiff(input: String, eses: String,

-                                         removeUnescapedEscapesRegex: String, removeEscapeEscapesThatEscapeRegex: String,

-                                         removeEscapeRegex: String): String = {

-    // TODO: Move regular expressions out into central class

-    if (eses.length() > 0) {

-      val removeUnescapedEscapes = removeUnescapedEscapesRegex //rRemoveUnescapedEscapes.format(eses, es)

-      val removeEscapeEscapesThatEscape = removeEscapeEscapesThatEscapeRegex //rRemoveEscapeEscapesThatEscape.format(eses, es)

-      val r1 = input.replaceAll(removeUnescapedEscapes, "")

-      val r2 = r1.replaceAll(removeEscapeEscapesThatEscape, "")

-      return r2

-    }

-    val rRemoveEscape = removeEscapeRegex

-    val r1 = input.replaceAll(rRemoveEscape, "")

-    r1

-  }

-

-  def parseInputNCharacters(inputNCharsParser: Parser[String], input: Reader[Char],

-                            removePaddingParser: Option[Parser[String]], justification: TextJustificationType.Type): DelimParseResult = {

-    // For debug can use this logging parser instead.

-    val res = this.parse(this.log(inputNCharsParser)("DelimParser.parseInputNCharacters"), input)

-

-    res match {

-      case s @ Success(field, next) => {

-        val fieldNoPadding = removePadding(removePaddingParser, justification, field)

-        DelimParseSuccessFactory(Success(fieldNoPadding, next), "", DelimiterType.NotDelimited, Some(field), DelimiterLocation.Local)

-      }

-      case f: NoSuccess => DelimParseFailure(f.msg, f.next)

-    }

-  }

-

-  def removePadding(removePaddingParser: Option[Parser[String]], justification: TextJustificationType.Type,

-                    input: String): String = {

-    val result = removePaddingParser match {

-      case Some(p) => {

-        val res = this.parse(this.log(p)({

-          justification match {

-            case TextJustificationType.Left => "DelimParser.removePadding.leftJustified"

-            case TextJustificationType.Right => "DelimParser.removePadding.rightJustified"

-            case TextJustificationType.Center => "DelimParser.removePadding.centerJustified"

-            case TextJustificationType.None => Assert.invariantFailed("should not be none if we're trimming.")

-          }

-        }), input)

-        res.getOrElse(input)

-      }

-      case None => input

-    }

-    result

-  }

-

-  def parseInputPatterned(patternParser: Parser[String], input: Reader[Char]): DelimParseResult = {

-

-    // FOR DEBUGGING might want this logging version

-    val res = this.parse(this.log(patternParser)("DelimParser.parseInputPatterned"), input)

-

-    res match {

-      case s @ Success(_, _) => DelimParseSuccessFactory(s, "", DelimiterType.NotDelimited, None, DelimiterLocation.Local)

-      case f: NoSuccess => DelimParseFailure(f.msg, f.next)

-    }

-  }

-

-}

+package edu.illinois.ncsa.daffodil.processors
+
+/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
+ *
+ * Developed by: Tresys Technology, LLC
+ *               http://www.tresys.com
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal with
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ * 
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimers.
+ * 
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimers in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *  3. Neither the names of Tresys Technology, nor the names of its contributors
+ *     may be used to endorse or promote products derived from this Software
+ *     without specific prior written permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
+ * SOFTWARE.
+ */
+
+import scala.util.parsing.combinator.RegexParsers
+import scala.util.parsing.input.Reader
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+
+class DFDLDelimParserStatic(stringBitLengthFunction: String => Int)
+  extends DFDLDelimParserCommon(stringBitLengthFunction) {
+
+  def parseInput(field: Parser[(Vector[String], String)], seps: Parser[String], terms: Parser[String],
+                 input: Reader[Char], justification: TextJustificationType.Type): DelimParseResult = {
+    val result = this.parseInputDefaultContent(field, seps, terms, input, justification)
+    result
+  }
+
+  def parseInputDelimiter(inputDelimiterParser: Parser[String], isLocalDelimParser: Parser[String],
+                          input: Reader[Char]): DelimParseResult = {
+    val res = this.parse(this.log(inputDelimiterParser)("DelimParser.parseInputDelimiter.allDelims"), input)
+
+    // TODO: This seems pretty inefficient. We're redoing a match in order to know 
+    // whether it was local or remote?? 
+    val result = res match {
+      case s @ Success(delimiterResult, next) => {
+        // We have a result but was it a remote or local match?
+
+        val subResult = this.parseAll(this.log(isLocalDelimParser)("DelimParser.parseInputDelimiter.isLocal"), delimiterResult)
+        val delimiterLocation = if (subResult.isEmpty) DelimiterLocation.Remote else DelimiterLocation.Local
+        //
+        // TODO: ?? Is None the right thing to pass here?? If we pass none, then it is 
+        // going to determine the length based on the delimiterResult. Does that include
+        // everything it needs to include?
+        //
+        DelimParseSuccessFactory(s, delimiterResult, DelimiterType.NotDelimited, None, // Is None right?
+          delimiterLocation)
+      }
+      case NoSuccess(msg, next) => DelimParseFailure(msg, next)
+    }
+    result
+  }
+
+  def parseInputEscapeBlock(escapeBlockParser: Parser[(Vector[String], String)], seps: Parser[String],
+                            terms: Parser[String], input: Reader[Char], justification: TextJustificationType.Type,
+                            removeEscapeBlocksRegex: String, parseInputParser: Parser[(Vector[String], String)]): DelimParseResult = {
+    val result1 = this.parseInputEscapeBlockContent(escapeBlockParser, seps, terms, input, justification)
+    val result2 = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapeBlocks(field, removeEscapeBlocksRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+      }
+      case f: DelimParseFailure => this.parseInputDefaultContent(parseInputParser, seps, terms,
+        input, justification)
+    }
+    result2
+  }
+
+  private def removeEscapeBlocks(input: String, removeEscapeBlockRegex: String): String = {
+    input.replaceAll(removeEscapeBlockRegex, "")
+  }
+
+  def parseInputEscapeCharacter(escapeCharacterParser: Parser[(Vector[String], String)],
+                                seps: Parser[String], terms: Parser[String], input: Reader[Char],
+                                justification: TextJustificationType.Type,
+                                removeEscapeCharacterRegex: scala.util.matching.Regex,
+                                removeUnescapedEscapesRegex: String,
+                                removeEscapeEscapesThatEscapeRegex: String,
+                                removeEscapeRegex: String, es: String, eses: String): DelimParseResult = {
+    val result1 = this.parseInputEscapeCharContent(escapeCharacterParser, seps, terms, input, justification)
+    val result2 = result1 match {
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val newField = removeEscapeCharacters(field, eses, es, removeEscapeCharacterRegex, removeUnescapedEscapesRegex,
+          removeEscapeEscapesThatEscapeRegex, removeEscapeRegex)
+        s.copy(fieldArg = newField, nextArg = s.next)
+      }
+      case f: DelimParseFailure => f
+    }
+    result2
+  }
+
+  /**
+   * Assumes 'input' has had its delimiter picked off end already if it existed.
+   */
+  private def removeEscapeCharacters(input: String, eses: String, es: String,
+                                     removeEscCharsSameRegex: scala.util.matching.Regex,
+                                     removeUnescapedEscapesRegex: String,
+                                     removeEscapeEscapesThatEscapeRegex: String,
+                                     removeEscapeRegex: String): String = {
+    if (eses.equals(es)) {
+      return removeEscapeCharactersSame(input, removeEscCharsSameRegex)
+    } else {
+      return removeEscapeCharactersDiff(input, eses, removeUnescapedEscapesRegex,
+        removeEscapeEscapesThatEscapeRegex, removeEscapeRegex)
+    }
+  }
+
+  private def removeEscapeCharactersSame(input: String,
+                                         removeEscCharsSameRegex: scala.util.matching.Regex): String = {
+    // used to cleanup escape characters 
+    val ERSplit = removeEscCharsSameRegex
+    def removeActiveEscapes(str: String): String = {
+      val res = str match {
+        case ERSplit(before, theEsc, delim, after) => {
+          val rest = removeActiveEscapes(after)
+          before + delim + rest
+        }
+        case ERSplit(before, delim, after) => {
+          val rest = removeActiveEscapes(after)
+          before + delim + rest
+        }
+        case _ => str
+      }
+      res
+    }
+    removeActiveEscapes(input)
+  }
+
+  private def removeEscapeCharactersDiff(input: String, eses: String,
+                                         removeUnescapedEscapesRegex: String, removeEscapeEscapesThatEscapeRegex: String,
+                                         removeEscapeRegex: String): String = {
+    // TODO: Move regular expressions out into central class
+    if (eses.length() > 0) {
+      val removeUnescapedEscapes = removeUnescapedEscapesRegex //rRemoveUnescapedEscapes.format(eses, es)
+      val removeEscapeEscapesThatEscape = removeEscapeEscapesThatEscapeRegex //rRemoveEscapeEscapesThatEscape.format(eses, es)
+      val r1 = input.replaceAll(removeUnescapedEscapes, "")
+      val r2 = r1.replaceAll(removeEscapeEscapesThatEscape, "")
+      return r2
+    }
+    val rRemoveEscape = removeEscapeRegex
+    val r1 = input.replaceAll(rRemoveEscape, "")
+    r1
+  }
+
+  def parseInputNCharacters(inputNCharsParser: Parser[String], input: Reader[Char],
+                            removePaddingParser: Option[Parser[String]], justification: TextJustificationType.Type): DelimParseResult = {
+    // For debug can use this logging parser instead.
+    val res = this.parse(this.log(inputNCharsParser)("DelimParser.parseInputNCharacters"), input)
+
+    res match {
+      case s @ Success(field, next) => {
+        val fieldNoPadding = removePadding(removePaddingParser, justification, field)
+        DelimParseSuccessFactory(Success(fieldNoPadding, next), "", DelimiterType.NotDelimited, Some(field), DelimiterLocation.Local)
+      }
+      case f: NoSuccess => DelimParseFailure(f.msg, f.next)
+    }
+  }
+
+  def removePadding(removePaddingParser: Option[Parser[String]], justification: TextJustificationType.Type,
+                    input: String): String = {
+    val result = removePaddingParser match {
+      case Some(p) => {
+        val res = this.parse(this.log(p)({
+          justification match {
+            case TextJustificationType.Left => "DelimParser.removePadding.leftJustified"
+            case TextJustificationType.Right => "DelimParser.removePadding.rightJustified"
+            case TextJustificationType.Center => "DelimParser.removePadding.centerJustified"
+            case TextJustificationType.None => Assert.invariantFailed("should not be none if we're trimming.")
+          }
+        }), input)
+        res.getOrElse(input)
+      }
+      case None => input
+    }
+    result
+  }
+
+  def parseInputPatterned(patternParser: Parser[String], input: Reader[Char]): DelimParseResult = {
+
+    // FOR DEBUGGING might want this logging version
+    val res = this.parse(this.log(patternParser)("DelimParser.parseInputPatterned"), input)
+
+    res match {
+      case s @ Success(_, _) => DelimParseSuccessFactory(s, "", DelimiterType.NotDelimited, None, DelimiterLocation.Local)
+      case f: NoSuccess => DelimParseFailure(f.msg, f.next)
+    }
+  }
+
+}
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimiter.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimiter.scala
index 28e72bf..acbc9c3 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimiter.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLDelimiter.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.processors

-

+package edu.illinois.ncsa.daffodil.processors
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,521 +30,521 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import java.util.regex.Pattern

-import java.util.logging.Logging

-import scala.util.control.Breaks

-import java.util.regex.Matcher

-import scala.collection.mutable.Queue

-import edu.illinois.ncsa.daffodil.util.Enum

-

-object DelimiterType extends Enum {

-  sealed abstract trait Type extends EnumValueType

-  case object Separator extends Type

-  case object Terminator extends Type

-  case object NotDelimited extends Type

-}

-

-object DelimiterLocation extends Enum {

-  sealed abstract trait Type extends EnumValueType

-  case object Local extends Type

-  case object Remote extends Type

-}

-

-class Delimiter {

-  var delimiterStr: String = "" // String representation of delimiter Ex. "%WSP;,%WSP*;"

-

-  var delimBuf: Array[DelimBase] = Array.empty[DelimBase] /* Buffer where each cell (DelimBase) represents a character

-		  												     in the delimiter string */

-

-  var delimRegExParseDelim: String = "" // Regex to actually parse the entire delimiter

-

-  // Pre-compiled RegEx patterns for finding character classes

-  lazy val NL = Pattern.compile("%(NL);", Pattern.MULTILINE)

-  lazy val WSP = Pattern.compile("%(WSP);", Pattern.MULTILINE)

-  lazy val WSP_Plus = Pattern.compile("%(WSP\\+);", Pattern.MULTILINE)

-  lazy val WSP_Star = Pattern.compile("%(WSP\\*);", Pattern.MULTILINE)

-

-  override def toString(): String = {

-    return "Delimiter[" + delimiterStr + "]"

-  }

-

-  // Must call to create the necessary structures

-  //

-  def apply(pDelimiter: String) = {

-    delimiterStr = pDelimiter

-    delimBuf = buildDelimBuf(delimiterStr)

-

-    delimRegExParseDelim = this.delimRegexParseDelim(delimBuf)

-  }

-

-  // Reduces complicated delimiters containing consecutive WSP, WSP* and WSP+

-  // character classes.

-  //

-  // Ex. %WSP;%WSP*;%NL;%WSP+;%WSP*

-  // 	can be reduced to: %WSP+;%NL;%WSP+;

-  //

-  // TODO: Maybe should have an error message for the example.  What did they mean?

-  // Problem because NL characters are in WSP.  Possible to consume the expected NL

-  // and thus the rest of the delimiter may not match.

-  //

-  // Here we should note that %WSP;%WSP;%WSP; is NOT equivalent to %WSP+;

-  // as WSP+ would imply that %WSP;%WSP;%WSP;%WSP; is also valid when in fact

-  // it may not be.

-  //

-  def reduceDelimBuf(delims: Array[DelimBase]): Array[DelimBase] = {

-

-    val q: Queue[DelimBase] = new Queue[DelimBase]()

-

-    // Counters to keep track of WSP,+,* objects

-    var numWSP: Int = 0

-    var numWSP_Plus: Int = 0

-    var numWSP_Star: Int = 0

-

-    var idx: Int = 0 // To index the resultant array

-

-    delims.foreach(delim => {

-      delim match {

-        case wsp: WSPDelim => numWSP += 1

-        case wsp: WSPPlusDelim => numWSP_Plus += 1

-        case wsp: WSPStarDelim => numWSP_Star += 1

-        case _ => {

-          // We've reached a non WSP delimiter, check if we've

-          // previously encountered any WSP delimiter objects and

-          // return the equivalent representation (if any)

-          val result = getReducedDelim(numWSP, numWSP_Plus, numWSP_Star)

-

-          result match {

-            case Some(x) => {

-              // WSP exists and an equivalent representation was found

-              x.index = idx // Set the delimiter's index

-              q += x

-              idx += 1

-            }

-            case None => {

-              // Reduction not possible, but did we come across

-              // more than one WSP?

-

-              var i = 0

-              while (i < numWSP) {

-                val wsp = new WSPDelim

-                wsp.index = idx

-                q += wsp

-                idx += 1

-                i += 1

-              }

-            }

-          }

-

-          // Set the delimiter's index, needed to

-          // update the delimBuf individual node (DelimBase) state later

-          delim.index = idx

-          q += delim

-          idx += 1

-

-          // Reset counters

-          numWSP = 0

-          numWSP_Plus = 0

-          numWSP_Star = 0

-        }

-      }

-    }) // end-for-each

-

-    // Check for leftovers in case the delimiter

-    // ends in spaces

-    val result = getReducedDelim(numWSP, numWSP_Plus, numWSP_Star)

-

-    result match {

-      case Some(x) => {

-        x.index = idx

-        q += x

-      }

-      case None => {

-        // Reduction not possible, but did we come across

-        // more than one WSP?

-

-        var i = 0

-        while (i < numWSP) {

-          val wsp = new WSPDelim

-          wsp.index = idx

-          q += wsp

-          idx += 1

-          i += 1

-        }

-      }

-    }

-

-    q.toArray[DelimBase]

-  }

-

-  // Based upon what WSP delimiters were encountered,

-  // determine the equivalent representation (if any) and return it.

-  //

-  def getReducedDelim(numWSP: Int, numWSP_Plus: Int, numWSP_Star: Int): Option[DelimBase] = {

-    // 				TRUTH TABLE

-    //		WSP		WSP+	WSP*	RESULT

-    // 1	0		0		0		NONE

-    // 2	0		0		1		WSP*

-    // 3	0		1		0		WSP+

-    // 4	0		1		1		WSP+

-    // 5	1		0		0		WSP

-    // 6	1		0		1		WSP+

-    // 7	1		1		0		WSP+

-    // 8	1		1		1		WSP+

-    if (numWSP_Plus != 0) {

-      // Case: 3, 4, 7, 8

-      return Some(new WSPPlusDelim())

-    } else if (numWSP != 0 && numWSP_Plus == 0 && numWSP_Star != 0) { // WSP+ == 0

-      // Case: 6

-      return Some(new WSPPlusDelim())

-    } else if (numWSP == 0 && numWSP_Plus == 0 && numWSP_Star != 0) {

-      // Case: 2

-      return Some(new WSPStarDelim())

-    } else if (numWSP == 1 && numWSP_Plus == 0 && numWSP_Star == 0) {

-      // Case: 5

-      return Some(new WSPDelim())

-    }

-    None

-  }

-

-  // Creates a RegEx representation of the delimiter.

-  // Important for comparing the actual delimiter against

-  // the data returned.

-  // Ex. separator = "%WSP*;,%WSP*;"

-  //	delimiter retrieved from data: ", "

-  // There is no way that the separator text can equate to the data

-  // when character classes are involved, RegEx allows us to determine

-  // if the delimiter/data was in the expected format.

-  //

-  def delimRegexParseDelim(delimiterBuf: Array[DelimBase] = delimBuf): String = {

-    var sb: StringBuilder = new StringBuilder

-    delimiterBuf foreach {

-      delim =>

-        {

-          delim match {

-            case nl: NLDelim => {

-              sb.append("(?>" + // Eliminates needles backtracking. Atomic group of

-                "(\\r\\n)|" + // CRLF

-                "((?<!\\r)\\n)|" + // LF not preceded by CR

-                "(\\r(?!\\n))|" + // CR not followed by LF

-                "\\u0085|\\u2028)")

-            }

-            case wsp: WSPDelim => {

-              sb.append("(\\s|\\u0020|\\u0009|\\u000A|\\u000B|\\u000C|\\u000D|\\u0085" +

-                "|\\u00A0|\\u1680|\\u180E|\\u2000|\\u2001|\\u2002|\\u2003|\\u2004|\\u2005|\\u2006|" +

-                "\\u2007|\\u2008|\\u2009|\\u200A|\\u2028|\\u2029|\\u202F|\\u205F|\\u3000)")

-            } // Single space

-            case wsp: WSPPlusDelim => {

-              sb.append("(\\s|\\u0020|\\u0009|\\u000A|\\u000B|\\u000C|\\u000D|\\u0085" +

-                "|\\u00A0|\\u1680|\\u180E|\\u2000|\\u2001|\\u2002|\\u2003|\\u2004|\\u2005|\\u2006|" +

-                "\\u2007|\\u2008|\\u2009|\\u200A|\\u2028|\\u2029|\\u202F|\\u205F|\\u3000)+")

-            } // One or more spaces

-            case wsp: WSPStarDelim => {

-              sb.append("(\\s|\\u0020|\\u0009|\\u000A|\\u000B|\\u000C|\\u000D|\\u0085" +

-                "|\\u00A0|\\u1680|\\u180E|\\u2000|\\u2001|\\u2002|\\u2003|\\u2004|\\u2005|\\u2006|" +

-                "\\u2007|\\u2008|\\u2009|\\u200A|\\u2028|\\u2029|\\u202F|\\u205F|\\u3000)*")

-            } // None or more spaces

-            case char: CharDelim => { // Some character

-              char.char match {

-                case '[' => sb.append("\\[")

-                case '\\' => sb.append("\\\\")

-                case '^' => sb.append("\\^")

-                case '$' => sb.append("\\$")

-                case '.' => sb.append("\\.")

-                case '|' => sb.append("\\|")

-                case '?' => sb.append("\\?")

-                case '*' => sb.append("\\*")

-                case '+' => sb.append("\\+")

-                case '(' => sb.append("\\(")

-                case ')' => sb.append("\\)")

-                case '{' => sb.append("\\{")

-                case '}' => sb.append("\\}")

-                case x => sb.append(x)

-              }

-            }

-          }

-        }

-    }

-    sb.toString()

-  }

-

-  // Returns the first character class in the String

-  // or None if one is not found

-  //

-  def findCharClasses(str: String): (Int, Option[DelimBase]) = {

-    val mNL: Matcher = NL.matcher(str)

-    val mWSP: Matcher = WSP.matcher(str)

-    val mWSP_Plus: Matcher = WSP_Plus.matcher(str)

-    val mWSP_Star: Matcher = WSP_Star.matcher(str)

-    var length: Int = -1

-

-    val classList: scala.collection.mutable.Map[String, (Int, Int)] = scala.collection.mutable.Map.empty

-

-    if (mNL.find()) {

-      classList += ("NL" -> (mNL.start() -> mNL.end()))

-    }

-

-    if (mWSP.find()) {

-      classList += ("WSP" -> (mWSP.start() -> mWSP.end()))

-    }

-

-    if (mWSP_Plus.find()) {

-      classList += ("WSP+" -> (mWSP_Plus.start() -> mWSP_Plus.end()))

-    }

-

-    if (mWSP_Star.find()) {

-      classList += ("WSP*" -> (mWSP_Star.start() -> mWSP_Star.end()))

-    }

-

-    if (classList.size > 0) {

-      val minItem = classList.minBy(x => x._2._1)

-      length = minItem._2._2 - minItem._2._1

-      val result = minItem._1 match {

-        case "NL" => (length, Some(new NLDelim()))

-        case "WSP" => (length, Some(new WSPDelim()))

-        case "WSP+" => (length, Some(new WSPPlusDelim()))

-        case "WSP*" => (length, Some(new WSPStarDelim()))

-      }

-      return result

-    }

-    (-1, None) // Unrecognized CharClass

-  }

-

-  // Populates the delimBuf object with an object

-  // representation of the characters within the delimiter

-  // string.

-  //

-  def buildDelimBuf(delimStr: String): Array[DelimBase] = {

-    val q: Queue[DelimBase] = new Queue[DelimBase]()

-    var inc = 0

-    val loop = new Breaks

-

-    var newIdx = 0 // index within delimBuf array

-

-    var numCharClass: Int = 0

-

-    loop.breakable {

-      for (i <- 0 until delimStr.length()) {

-        val idx = i + inc // Advances cursor past the Character Class

-

-        if (idx >= delimStr.length()) {

-          // ran off end of delimiter string, break!

-          loop.break()

-        }

-

-        val c: Char = delimStr.charAt(idx)

-

-        if (c == '%') {

-          // Possible character class, check patterns

-

-          // According to JavaDoc, split will always return at least

-          // one result even if there is no match.

-          val split = delimStr.substring(idx + 1).split("%")

-

-          val subStr: String = "%" + split(0)

-          val (matchLength, delimObj) = findCharClasses(subStr)

-

-          if (matchLength != -1) {

-            // Have a match, add the object

-            val obj = delimObj.get

-            obj.index = newIdx // Index within delimBuf Array

-            q += obj

-            inc += matchLength - 1 // advance cursor past the Character Class

-            newIdx += 1

-            numCharClass += 1

-          } else {

-            // Not a CharClass or unrecognized,

-            // therefore treat as a CharDelim

-            val obj = new CharDelim(c)

-            obj.index = newIdx // Index within delimBuf Array

-            newIdx += 1

-            q += obj

-          }

-

-        } else {

-          // A CharDelim

-          val obj = new CharDelim(c)

-          obj.index = newIdx // Index within delimBuf Array

-          newIdx += 1

-          q += obj

-        }

-      } // END for-loop

-    } // END loop-breakable

-    var resDelimBuf: Array[DelimBase] = null

-    if (numCharClass > 1) {

-      // More than one Char Class, reduction possible!

-      resDelimBuf = reduceDelimBuf(q.toArray[DelimBase])

-    } else {

-      // No need to reduce

-      resDelimBuf = q.toArray[DelimBase]

-    }

-    resDelimBuf

-  }

-}

-

-abstract class DelimBase extends Base {

-  def typeName: String

-  def print

-  def printStr: String

-  override def toString(): String = {

-    return typeName

-  }

-}

-

-trait Base {

-  var isMatched: Boolean = false

-  var index: Int = -1

-  var charPos: Int = -1

-  var charPosEnd: Int = -1

-

-  def clear = {

-    isMatched = false

-    charPos = -1

-    charPosEnd = -1

-  }

-

-  def checkMatch(charIn: Char): Boolean

-}

-

-class CharDelim(val char: Char) extends DelimBase {

-  def checkMatch(charIn: Char): Boolean = {

-    val matched = charIn == char

-    matched

-  }

-

-  lazy val typeName = "CharDelim"

-  def print = {

-    //log(LogLevel.Debug, "\t\t\t" + typeName + ": '" + char + "' d" + char.toInt + " isMatched: " + isMatched.toString()))

-  }

-

-  def printStr = {

-    val res = typeName + "(" + char + ")"

-    res

-  }

-

-  override def toString(): String = {

-    return typeName + "[" + char + "]"

-  }

-}

-

-trait CharacterClass {

-  def convertUnicodeToChar(unicode: String): Char = {

-    val c: Char = Integer.parseInt(unicode.substring(2), 16).asInstanceOf[Char]

-    c

-  }

-}

-

-class NLDelim extends DelimBase with CharacterClass {

-  lazy val typeName = "NLDelim"

-

-  lazy val LF: Char = { convertUnicodeToChar("\\u000A") }

-  lazy val CR: Char = { convertUnicodeToChar("\\u000D") }

-  lazy val NEL: Char = { convertUnicodeToChar("\\u0085") }

-  lazy val LS: Char = { convertUnicodeToChar("\\u2028") }

-

-  def checkMatch(charIn: Char): Boolean = {

-    charIn match {

-      case LF | CR | NEL | LS => isMatched = true

-      case _ => isMatched = false

-    }

-    isMatched

-  }

-

-  def print = {

-    //log(LogLevel.Debug, "\t\t\t" + typeName + ": NL" + " isMatched: " + isMatched.toString()))

-  }

-  def printStr = {

-    val res = typeName

-    res

-  }

-}

-

-trait WSP extends CharacterClass {

-  lazy val CTRL0: Char = { convertUnicodeToChar("\\u0009") }

-  lazy val CTRL1: Char = { convertUnicodeToChar("\\u000A") }

-  lazy val CTRL2: Char = { convertUnicodeToChar("\\u000B") }

-  lazy val CTRL3: Char = { convertUnicodeToChar("\\u000C") }

-  lazy val CTRL4: Char = { convertUnicodeToChar("\\u000D") }

-

-  lazy val SPACE: Char = { convertUnicodeToChar("\\u0020") }

-

-  lazy val NEL: Char = { convertUnicodeToChar("\\u0085") }

-

-  lazy val NBSP: Char = { convertUnicodeToChar("\\u00A0") }

-

-  lazy val OGHAM: Char = { convertUnicodeToChar("\\u1680") }

-  lazy val MONG: Char = { convertUnicodeToChar("\\u180E") }

-

-  lazy val SP0: Char = { convertUnicodeToChar("\\u2000") }

-  lazy val SP1: Char = { convertUnicodeToChar("\\u2001") }

-  lazy val SP2: Char = { convertUnicodeToChar("\\u2002") }

-  lazy val SP3: Char = { convertUnicodeToChar("\\u2003") }

-  lazy val SP4: Char = { convertUnicodeToChar("\\u2004") }

-  lazy val SP5: Char = { convertUnicodeToChar("\\u2005") }

-  lazy val SP6: Char = { convertUnicodeToChar("\\u2006") }

-  lazy val SP7: Char = { convertUnicodeToChar("\\u2007") }

-  lazy val SP8: Char = { convertUnicodeToChar("\\u2008") }

-  lazy val SP9: Char = { convertUnicodeToChar("\\u2009") }

-  lazy val SP10: Char = { convertUnicodeToChar("\\u200A") }

-

-  lazy val LSP: Char = { convertUnicodeToChar("\\u2028") }

-  lazy val PSP: Char = { convertUnicodeToChar("\\u2029") }

-  lazy val NARROW: Char = { convertUnicodeToChar("\\u202F") }

-  lazy val MED: Char = { convertUnicodeToChar("\\u205F") }

-  lazy val IDE: Char = { convertUnicodeToChar("\\u3000") }

-}

-

-class WSPBase extends DelimBase with WSP {

-  lazy val typeName = "WSPBase"

-  def checkMatch(charIn: Char): Boolean = {

-    charIn match {

-      case CTRL0 | CTRL1 | CTRL2 | CTRL3 | CTRL4 => isMatched = true

-      case SPACE | NEL | NBSP | OGHAM | MONG => isMatched = true

-      case SP0 | SP1 | SP2 | SP3 | SP4 | SP5 | SP6 | SP7 | SP8 | SP9 | SP10 => isMatched = true

-      case LSP | PSP | NARROW | MED | IDE => isMatched = true

-      case _ => isMatched = false

-    }

-    isMatched

-  }

-  def print = {

-    //log(LogLevel.Debug, "\t\t\t" + typeName + ": WSPBase" + " isMatched: " + isMatched.toString()))

-  }

-  def printStr = {

-    val res = typeName

-    res

-  }

-}

-

-class WSPDelim extends WSPBase with WSP {

-  override lazy val typeName = "WSPDelim"

-  override def print = {

-    //log(LogLevel.Debug, "\t\t\t" + typeName + ": WSP" + " isMatched: " + isMatched.toString()))

-  }

-  override def printStr = {

-    val res = typeName

-    res

-  }

-}

-

-class WSPPlusDelim extends WSPBase with WSP {

-  override lazy val typeName = "WSP+Delim"

-  override def print = {

-    //log(LogLevel.Debug, "\t\t\t" + typeName + ": WSP+" + " isMatched: " + isMatched.toString()))

-  }

-  override def printStr = {

-    val res = typeName

-    res

-  }

-}

-

-class WSPStarDelim extends WSPBase with WSP {

-  override lazy val typeName = "WSP*Delim"

-  override def print = {

-    //log(LogLevel.Debug, "\t\t\t" + typeName + ": WSP*" + " isMatched: " + isMatched.toString()))

-  }

-  override def printStr = {

-    val res = typeName

-    res

-  }

-}

+ */
+
+import java.util.regex.Pattern
+import java.util.logging.Logging
+import scala.util.control.Breaks
+import java.util.regex.Matcher
+import scala.collection.mutable.Queue
+import edu.illinois.ncsa.daffodil.util.Enum
+
+object DelimiterType extends Enum {
+  sealed abstract trait Type extends EnumValueType
+  case object Separator extends Type
+  case object Terminator extends Type
+  case object NotDelimited extends Type
+}
+
+object DelimiterLocation extends Enum {
+  sealed abstract trait Type extends EnumValueType
+  case object Local extends Type
+  case object Remote extends Type
+}
+
+class Delimiter {
+  var delimiterStr: String = "" // String representation of delimiter Ex. "%WSP;,%WSP*;"
+
+  var delimBuf: Array[DelimBase] = Array.empty[DelimBase] /* Buffer where each cell (DelimBase) represents a character
+		  												     in the delimiter string */
+
+  var delimRegExParseDelim: String = "" // Regex to actually parse the entire delimiter
+
+  // Pre-compiled RegEx patterns for finding character classes
+  lazy val NL = Pattern.compile("%(NL);", Pattern.MULTILINE)
+  lazy val WSP = Pattern.compile("%(WSP);", Pattern.MULTILINE)
+  lazy val WSP_Plus = Pattern.compile("%(WSP\\+);", Pattern.MULTILINE)
+  lazy val WSP_Star = Pattern.compile("%(WSP\\*);", Pattern.MULTILINE)
+
+  override def toString(): String = {
+    return "Delimiter[" + delimiterStr + "]"
+  }
+
+  // Must call to create the necessary structures
+  //
+  def apply(pDelimiter: String) = {
+    delimiterStr = pDelimiter
+    delimBuf = buildDelimBuf(delimiterStr)
+
+    delimRegExParseDelim = this.delimRegexParseDelim(delimBuf)
+  }
+
+  // Reduces complicated delimiters containing consecutive WSP, WSP* and WSP+
+  // character classes.
+  //
+  // Ex. %WSP;%WSP*;%NL;%WSP+;%WSP*
+  // 	can be reduced to: %WSP+;%NL;%WSP+;
+  //
+  // TODO: Maybe should have an error message for the example.  What did they mean?
+  // Problem because NL characters are in WSP.  Possible to consume the expected NL
+  // and thus the rest of the delimiter may not match.
+  //
+  // Here we should note that %WSP;%WSP;%WSP; is NOT equivalent to %WSP+;
+  // as WSP+ would imply that %WSP;%WSP;%WSP;%WSP; is also valid when in fact
+  // it may not be.
+  //
+  def reduceDelimBuf(delims: Array[DelimBase]): Array[DelimBase] = {
+
+    val q: Queue[DelimBase] = new Queue[DelimBase]()
+
+    // Counters to keep track of WSP,+,* objects
+    var numWSP: Int = 0
+    var numWSP_Plus: Int = 0
+    var numWSP_Star: Int = 0
+
+    var idx: Int = 0 // To index the resultant array
+
+    delims.foreach(delim => {
+      delim match {
+        case wsp: WSPDelim => numWSP += 1
+        case wsp: WSPPlusDelim => numWSP_Plus += 1
+        case wsp: WSPStarDelim => numWSP_Star += 1
+        case _ => {
+          // We've reached a non WSP delimiter, check if we've
+          // previously encountered any WSP delimiter objects and
+          // return the equivalent representation (if any)
+          val result = getReducedDelim(numWSP, numWSP_Plus, numWSP_Star)
+
+          result match {
+            case Some(x) => {
+              // WSP exists and an equivalent representation was found
+              x.index = idx // Set the delimiter's index
+              q += x
+              idx += 1
+            }
+            case None => {
+              // Reduction not possible, but did we come across
+              // more than one WSP?
+
+              var i = 0
+              while (i < numWSP) {
+                val wsp = new WSPDelim
+                wsp.index = idx
+                q += wsp
+                idx += 1
+                i += 1
+              }
+            }
+          }
+
+          // Set the delimiter's index, needed to
+          // update the delimBuf individual node (DelimBase) state later
+          delim.index = idx
+          q += delim
+          idx += 1
+
+          // Reset counters
+          numWSP = 0
+          numWSP_Plus = 0
+          numWSP_Star = 0
+        }
+      }
+    }) // end-for-each
+
+    // Check for leftovers in case the delimiter
+    // ends in spaces
+    val result = getReducedDelim(numWSP, numWSP_Plus, numWSP_Star)
+
+    result match {
+      case Some(x) => {
+        x.index = idx
+        q += x
+      }
+      case None => {
+        // Reduction not possible, but did we come across
+        // more than one WSP?
+
+        var i = 0
+        while (i < numWSP) {
+          val wsp = new WSPDelim
+          wsp.index = idx
+          q += wsp
+          idx += 1
+          i += 1
+        }
+      }
+    }
+
+    q.toArray[DelimBase]
+  }
+
+  // Based upon what WSP delimiters were encountered,
+  // determine the equivalent representation (if any) and return it.
+  //
+  def getReducedDelim(numWSP: Int, numWSP_Plus: Int, numWSP_Star: Int): Option[DelimBase] = {
+    // 				TRUTH TABLE
+    //		WSP		WSP+	WSP*	RESULT
+    // 1	0		0		0		NONE
+    // 2	0		0		1		WSP*
+    // 3	0		1		0		WSP+
+    // 4	0		1		1		WSP+
+    // 5	1		0		0		WSP
+    // 6	1		0		1		WSP+
+    // 7	1		1		0		WSP+
+    // 8	1		1		1		WSP+
+    if (numWSP_Plus != 0) {
+      // Case: 3, 4, 7, 8
+      return Some(new WSPPlusDelim())
+    } else if (numWSP != 0 && numWSP_Plus == 0 && numWSP_Star != 0) { // WSP+ == 0
+      // Case: 6
+      return Some(new WSPPlusDelim())
+    } else if (numWSP == 0 && numWSP_Plus == 0 && numWSP_Star != 0) {
+      // Case: 2
+      return Some(new WSPStarDelim())
+    } else if (numWSP == 1 && numWSP_Plus == 0 && numWSP_Star == 0) {
+      // Case: 5
+      return Some(new WSPDelim())
+    }
+    None
+  }
+
+  // Creates a RegEx representation of the delimiter.
+  // Important for comparing the actual delimiter against
+  // the data returned.
+  // Ex. separator = "%WSP*;,%WSP*;"
+  //	delimiter retrieved from data: ", "
+  // There is no way that the separator text can equate to the data
+  // when character classes are involved, RegEx allows us to determine
+  // if the delimiter/data was in the expected format.
+  //
+  def delimRegexParseDelim(delimiterBuf: Array[DelimBase] = delimBuf): String = {
+    var sb: StringBuilder = new StringBuilder
+    delimiterBuf foreach {
+      delim =>
+        {
+          delim match {
+            case nl: NLDelim => {
+              sb.append("(?>" + // Eliminates needles backtracking. Atomic group of
+                "(\\r\\n)|" + // CRLF
+                "((?<!\\r)\\n)|" + // LF not preceded by CR
+                "(\\r(?!\\n))|" + // CR not followed by LF
+                "\\u0085|\\u2028)")
+            }
+            case wsp: WSPDelim => {
+              sb.append("(\\s|\\u0020|\\u0009|\\u000A|\\u000B|\\u000C|\\u000D|\\u0085" +
+                "|\\u00A0|\\u1680|\\u180E|\\u2000|\\u2001|\\u2002|\\u2003|\\u2004|\\u2005|\\u2006|" +
+                "\\u2007|\\u2008|\\u2009|\\u200A|\\u2028|\\u2029|\\u202F|\\u205F|\\u3000)")
+            } // Single space
+            case wsp: WSPPlusDelim => {
+              sb.append("(\\s|\\u0020|\\u0009|\\u000A|\\u000B|\\u000C|\\u000D|\\u0085" +
+                "|\\u00A0|\\u1680|\\u180E|\\u2000|\\u2001|\\u2002|\\u2003|\\u2004|\\u2005|\\u2006|" +
+                "\\u2007|\\u2008|\\u2009|\\u200A|\\u2028|\\u2029|\\u202F|\\u205F|\\u3000)+")
+            } // One or more spaces
+            case wsp: WSPStarDelim => {
+              sb.append("(\\s|\\u0020|\\u0009|\\u000A|\\u000B|\\u000C|\\u000D|\\u0085" +
+                "|\\u00A0|\\u1680|\\u180E|\\u2000|\\u2001|\\u2002|\\u2003|\\u2004|\\u2005|\\u2006|" +
+                "\\u2007|\\u2008|\\u2009|\\u200A|\\u2028|\\u2029|\\u202F|\\u205F|\\u3000)*")
+            } // None or more spaces
+            case char: CharDelim => { // Some character
+              char.char match {
+                case '[' => sb.append("\\[")
+                case '\\' => sb.append("\\\\")
+                case '^' => sb.append("\\^")
+                case '$' => sb.append("\\$")
+                case '.' => sb.append("\\.")
+                case '|' => sb.append("\\|")
+                case '?' => sb.append("\\?")
+                case '*' => sb.append("\\*")
+                case '+' => sb.append("\\+")
+                case '(' => sb.append("\\(")
+                case ')' => sb.append("\\)")
+                case '{' => sb.append("\\{")
+                case '}' => sb.append("\\}")
+                case x => sb.append(x)
+              }
+            }
+          }
+        }
+    }
+    sb.toString()
+  }
+
+  // Returns the first character class in the String
+  // or None if one is not found
+  //
+  def findCharClasses(str: String): (Int, Option[DelimBase]) = {
+    val mNL: Matcher = NL.matcher(str)
+    val mWSP: Matcher = WSP.matcher(str)
+    val mWSP_Plus: Matcher = WSP_Plus.matcher(str)
+    val mWSP_Star: Matcher = WSP_Star.matcher(str)
+    var length: Int = -1
+
+    val classList: scala.collection.mutable.Map[String, (Int, Int)] = scala.collection.mutable.Map.empty
+
+    if (mNL.find()) {
+      classList += ("NL" -> (mNL.start() -> mNL.end()))
+    }
+
+    if (mWSP.find()) {
+      classList += ("WSP" -> (mWSP.start() -> mWSP.end()))
+    }
+
+    if (mWSP_Plus.find()) {
+      classList += ("WSP+" -> (mWSP_Plus.start() -> mWSP_Plus.end()))
+    }
+
+    if (mWSP_Star.find()) {
+      classList += ("WSP*" -> (mWSP_Star.start() -> mWSP_Star.end()))
+    }
+
+    if (classList.size > 0) {
+      val minItem = classList.minBy(x => x._2._1)
+      length = minItem._2._2 - minItem._2._1
+      val result = minItem._1 match {
+        case "NL" => (length, Some(new NLDelim()))
+        case "WSP" => (length, Some(new WSPDelim()))
+        case "WSP+" => (length, Some(new WSPPlusDelim()))
+        case "WSP*" => (length, Some(new WSPStarDelim()))
+      }
+      return result
+    }
+    (-1, None) // Unrecognized CharClass
+  }
+
+  // Populates the delimBuf object with an object
+  // representation of the characters within the delimiter
+  // string.
+  //
+  def buildDelimBuf(delimStr: String): Array[DelimBase] = {
+    val q: Queue[DelimBase] = new Queue[DelimBase]()
+    var inc = 0
+    val loop = new Breaks
+
+    var newIdx = 0 // index within delimBuf array
+
+    var numCharClass: Int = 0
+
+    loop.breakable {
+      for (i <- 0 until delimStr.length()) {
+        val idx = i + inc // Advances cursor past the Character Class
+
+        if (idx >= delimStr.length()) {
+          // ran off end of delimiter string, break!
+          loop.break()
+        }
+
+        val c: Char = delimStr.charAt(idx)
+
+        if (c == '%') {
+          // Possible character class, check patterns
+
+          // According to JavaDoc, split will always return at least
+          // one result even if there is no match.
+          val split = delimStr.substring(idx + 1).split("%")
+
+          val subStr: String = "%" + split(0)
+          val (matchLength, delimObj) = findCharClasses(subStr)
+
+          if (matchLength != -1) {
+            // Have a match, add the object
+            val obj = delimObj.get
+            obj.index = newIdx // Index within delimBuf Array
+            q += obj
+            inc += matchLength - 1 // advance cursor past the Character Class
+            newIdx += 1
+            numCharClass += 1
+          } else {
+            // Not a CharClass or unrecognized,
+            // therefore treat as a CharDelim
+            val obj = new CharDelim(c)
+            obj.index = newIdx // Index within delimBuf Array
+            newIdx += 1
+            q += obj
+          }
+
+        } else {
+          // A CharDelim
+          val obj = new CharDelim(c)
+          obj.index = newIdx // Index within delimBuf Array
+          newIdx += 1
+          q += obj
+        }
+      } // END for-loop
+    } // END loop-breakable
+    var resDelimBuf: Array[DelimBase] = null
+    if (numCharClass > 1) {
+      // More than one Char Class, reduction possible!
+      resDelimBuf = reduceDelimBuf(q.toArray[DelimBase])
+    } else {
+      // No need to reduce
+      resDelimBuf = q.toArray[DelimBase]
+    }
+    resDelimBuf
+  }
+}
+
+abstract class DelimBase extends Base {
+  def typeName: String
+  def print
+  def printStr: String
+  override def toString(): String = {
+    return typeName
+  }
+}
+
+trait Base {
+  var isMatched: Boolean = false
+  var index: Int = -1
+  var charPos: Int = -1
+  var charPosEnd: Int = -1
+
+  def clear = {
+    isMatched = false
+    charPos = -1
+    charPosEnd = -1
+  }
+
+  def checkMatch(charIn: Char): Boolean
+}
+
+class CharDelim(val char: Char) extends DelimBase {
+  def checkMatch(charIn: Char): Boolean = {
+    val matched = charIn == char
+    matched
+  }
+
+  lazy val typeName = "CharDelim"
+  def print = {
+    //log(LogLevel.Debug, "\t\t\t" + typeName + ": '" + char + "' d" + char.toInt + " isMatched: " + isMatched.toString()))
+  }
+
+  def printStr = {
+    val res = typeName + "(" + char + ")"
+    res
+  }
+
+  override def toString(): String = {
+    return typeName + "[" + char + "]"
+  }
+}
+
+trait CharacterClass {
+  def convertUnicodeToChar(unicode: String): Char = {
+    val c: Char = Integer.parseInt(unicode.substring(2), 16).asInstanceOf[Char]
+    c
+  }
+}
+
+class NLDelim extends DelimBase with CharacterClass {
+  lazy val typeName = "NLDelim"
+
+  lazy val LF: Char = { convertUnicodeToChar("\\u000A") }
+  lazy val CR: Char = { convertUnicodeToChar("\\u000D") }
+  lazy val NEL: Char = { convertUnicodeToChar("\\u0085") }
+  lazy val LS: Char = { convertUnicodeToChar("\\u2028") }
+
+  def checkMatch(charIn: Char): Boolean = {
+    charIn match {
+      case LF | CR | NEL | LS => isMatched = true
+      case _ => isMatched = false
+    }
+    isMatched
+  }
+
+  def print = {
+    //log(LogLevel.Debug, "\t\t\t" + typeName + ": NL" + " isMatched: " + isMatched.toString()))
+  }
+  def printStr = {
+    val res = typeName
+    res
+  }
+}
+
+trait WSP extends CharacterClass {
+  lazy val CTRL0: Char = { convertUnicodeToChar("\\u0009") }
+  lazy val CTRL1: Char = { convertUnicodeToChar("\\u000A") }
+  lazy val CTRL2: Char = { convertUnicodeToChar("\\u000B") }
+  lazy val CTRL3: Char = { convertUnicodeToChar("\\u000C") }
+  lazy val CTRL4: Char = { convertUnicodeToChar("\\u000D") }
+
+  lazy val SPACE: Char = { convertUnicodeToChar("\\u0020") }
+
+  lazy val NEL: Char = { convertUnicodeToChar("\\u0085") }
+
+  lazy val NBSP: Char = { convertUnicodeToChar("\\u00A0") }
+
+  lazy val OGHAM: Char = { convertUnicodeToChar("\\u1680") }
+  lazy val MONG: Char = { convertUnicodeToChar("\\u180E") }
+
+  lazy val SP0: Char = { convertUnicodeToChar("\\u2000") }
+  lazy val SP1: Char = { convertUnicodeToChar("\\u2001") }
+  lazy val SP2: Char = { convertUnicodeToChar("\\u2002") }
+  lazy val SP3: Char = { convertUnicodeToChar("\\u2003") }
+  lazy val SP4: Char = { convertUnicodeToChar("\\u2004") }
+  lazy val SP5: Char = { convertUnicodeToChar("\\u2005") }
+  lazy val SP6: Char = { convertUnicodeToChar("\\u2006") }
+  lazy val SP7: Char = { convertUnicodeToChar("\\u2007") }
+  lazy val SP8: Char = { convertUnicodeToChar("\\u2008") }
+  lazy val SP9: Char = { convertUnicodeToChar("\\u2009") }
+  lazy val SP10: Char = { convertUnicodeToChar("\\u200A") }
+
+  lazy val LSP: Char = { convertUnicodeToChar("\\u2028") }
+  lazy val PSP: Char = { convertUnicodeToChar("\\u2029") }
+  lazy val NARROW: Char = { convertUnicodeToChar("\\u202F") }
+  lazy val MED: Char = { convertUnicodeToChar("\\u205F") }
+  lazy val IDE: Char = { convertUnicodeToChar("\\u3000") }
+}
+
+class WSPBase extends DelimBase with WSP {
+  lazy val typeName = "WSPBase"
+  def checkMatch(charIn: Char): Boolean = {
+    charIn match {
+      case CTRL0 | CTRL1 | CTRL2 | CTRL3 | CTRL4 => isMatched = true
+      case SPACE | NEL | NBSP | OGHAM | MONG => isMatched = true
+      case SP0 | SP1 | SP2 | SP3 | SP4 | SP5 | SP6 | SP7 | SP8 | SP9 | SP10 => isMatched = true
+      case LSP | PSP | NARROW | MED | IDE => isMatched = true
+      case _ => isMatched = false
+    }
+    isMatched
+  }
+  def print = {
+    //log(LogLevel.Debug, "\t\t\t" + typeName + ": WSPBase" + " isMatched: " + isMatched.toString()))
+  }
+  def printStr = {
+    val res = typeName
+    res
+  }
+}
+
+class WSPDelim extends WSPBase with WSP {
+  override lazy val typeName = "WSPDelim"
+  override def print = {
+    //log(LogLevel.Debug, "\t\t\t" + typeName + ": WSP" + " isMatched: " + isMatched.toString()))
+  }
+  override def printStr = {
+    val res = typeName
+    res
+  }
+}
+
+class WSPPlusDelim extends WSPBase with WSP {
+  override lazy val typeName = "WSP+Delim"
+  override def print = {
+    //log(LogLevel.Debug, "\t\t\t" + typeName + ": WSP+" + " isMatched: " + isMatched.toString()))
+  }
+  override def printStr = {
+    val res = typeName
+    res
+  }
+}
+
+class WSPStarDelim extends WSPBase with WSP {
+  override lazy val typeName = "WSP*Delim"
+  override def print = {
+    //log(LogLevel.Debug, "\t\t\t" + typeName + ": WSP*" + " isMatched: " + isMatched.toString()))
+  }
+  override def printStr = {
+    val res = typeName
+    res
+  }
+}
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLJavaIOInputStreamReader.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLJavaIOInputStreamReader.scala
index af0af08..3c36f31 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLJavaIOInputStreamReader.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLJavaIOInputStreamReader.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.processors

+package edu.illinois.ncsa.daffodil.processors
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,32 +32,32 @@
  * SOFTWARE.
  */
 
-

-import java.nio.charset.Charset

-import java.nio.charset.CharsetDecoder

-import java.io.InputStream

-

-object DFDLJavaIOInputStreamReader {

-

-  def apply(in: InputStream, charset: Charset, bitOffset0to7: Int, bitLimit: Long) = {

-    new DFDLJavaIOInputStreamReader(in, DFDLJavaIOStreamDecoder.forInputStreamReader(in, charset, bitOffset0to7, bitLimit))

-  }

-

-}

-

-/**

- * This class was ported from java in order to use our customized

- * version of the StreamDecoder.

- *

- * This was necessary because in DFDL we want malformed input to be

- * treated as the end of a data stream.

- */

-class DFDLJavaIOInputStreamReader private (val in: InputStream, val sd: DFDLJavaIOStreamDecoder)

-  extends java.io.Reader(in: InputStream) {

-

-  def getEncoding: String = sd.getEncoding

-  override def read: Int = sd.read()

-  def read(cbuf: Array[Char], offset: Int, length: Int): Int = sd.read(cbuf, offset, length)

-  override def ready: Boolean = sd.ready()

-  def close: Unit = sd.close()

-}

+
+import java.nio.charset.Charset
+import java.nio.charset.CharsetDecoder
+import java.io.InputStream
+
+object DFDLJavaIOInputStreamReader {
+
+  def apply(in: InputStream, charset: Charset, bitOffset0to7: Int, bitLimit: Long) = {
+    new DFDLJavaIOInputStreamReader(in, DFDLJavaIOStreamDecoder.forInputStreamReader(in, charset, bitOffset0to7, bitLimit))
+  }
+
+}
+
+/**
+ * This class was ported from java in order to use our customized
+ * version of the StreamDecoder.
+ *
+ * This was necessary because in DFDL we want malformed input to be
+ * treated as the end of a data stream.
+ */
+class DFDLJavaIOInputStreamReader private (val in: InputStream, val sd: DFDLJavaIOStreamDecoder)
+  extends java.io.Reader(in: InputStream) {
+
+  def getEncoding: String = sd.getEncoding
+  override def read: Int = sd.read()
+  def read(cbuf: Array[Char], offset: Int, length: Int): Int = sd.read(cbuf, offset, length)
+  override def ready: Boolean = sd.ready()
+  def close: Unit = sd.close()
+}
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLJavaIOStreamDecoder.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLJavaIOStreamDecoder.scala
index 21096a8..3c5b8b2 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLJavaIOStreamDecoder.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLJavaIOStreamDecoder.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.processors

-

+package edu.illinois.ncsa.daffodil.processors
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,384 +30,384 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import java.nio.CharBuffer

-import java.io.FileInputStream

-import java.nio.charset.Charset

-import sun.nio.cs.HistoricallyNamedCharset

-import java.nio.channels.ReadableByteChannel

-import java.nio.channels.Channels

-import java.io.IOException

-import java.nio.channels.FileChannel

-import java.nio.charset.CharsetDecoder

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import java.nio.charset.CodingErrorAction

-import java.nio.ByteBuffer

-import java.io.InputStream

-import scala.util.control.Breaks._

-import java.nio.charset.CoderResult

-import edu.illinois.ncsa.daffodil.processors.charset.USASCII7BitPackedCharset

-import edu.illinois.ncsa.daffodil.processors.charset.CharsetUtils

-import edu.illinois.ncsa.daffodil.processors.charset.SupportsInitialBitOffset

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import java.nio.charset.MalformedInputException

-import edu.illinois.ncsa.daffodil.processors.charset.CharacterSetAlignmentError

-

-/**

- * The purpose of re-implementing this class is to gain control over

- * how the StreamDecoder handles malformed input.  In DFDL we want the

- * malformed input error to be treated as the end of data.  Java's

- * StreamDecoder only ignores, replaces or treats it as an error.

- *

- * Mostly this class tries to remain true to the Java code from which it was derived

- * so as to preserve future inter-operation potential. However, some modifications

- * (for bit-level positioning) make that a lot less likely.

- */

-object DFDLJavaIOStreamDecoder {

-

-  private val DEFAULT_BYTE_BUFFER_SIZE: Int = Compiler.readerByteBufferSize.toInt

-

-  def forInputStreamReader(in: InputStream, charset: Charset, bitOffset0to7: Int, bitLimit: Long): DFDLJavaIOStreamDecoder = {

-

-    val myBB = ByteBuffer.allocateDirect(DFDLJavaIOStreamDecoder.DEFAULT_BYTE_BUFFER_SIZE)

-    myBB.flip()

-

-    val chan = in match {

-      case fis: FileInputStream => fis.getChannel()

-      case _ => Channels.newChannel(in)

-    }

-

-    new DFDLJavaIOStreamDecoder(bitOffset0to7, bitLimit, charset, myBB, in, chan)

-  }

-

-}

-

-/**

- * The purpose of re-implementing this class is to gain control over

- * how the StreamDecoder handles malformed input.  In DFDL we want the

- * malformed input error to be treated as the end of data.  Java's

- * StreamDecoder only ignores, replaces or treats it as an error.

- *

- * Forces the decoder to REPORT on malformed input.

- *

- * We also have to implment the upper-bound aka bitLimit, and not

- * allow consumption of data past it.

- */

-class DFDLJavaIOStreamDecoder private (bitOffsetWithinAByte: Int, val bitLimit: Long, var cs: Charset, var bb: ByteBuffer,

-  var in: InputStream, var ch: ReadableByteChannel)

-  extends java.io.Reader {

-

-  Assert.usage(ch != null)

-  Assert.usage(bitOffsetWithinAByte >= 0)

-  Assert.usage(bitOffsetWithinAByte <= 7)

-

-  val decoder = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT)

-

-  //

-  // Now we deal with bit positioning. If the decoder is capable of dealing with a starting bit offset

-  // then we configure it that way. If not we insist we are byte aligned and fail if not. 

-  //

-  decoder match {

-    case decoderWithBits: SupportsInitialBitOffset =>

-      decoderWithBits.setInitialBitOffset(bitOffsetWithinAByte)

-    case _ if (bitOffsetWithinAByte == 0) => // ok. Do nothing. We are luckily, aligned to a byte.

-    // We're counting on the parser surrounding us to catch this error and turn it into 

-    // a ParseError.

-    case _ => throw new CharacterSetAlignmentError(cs.name, 8, bitOffsetWithinAByte)

-  }

-

-  @volatile

-  private var isOpen: Boolean = true

-  def ensureOpen = { if (!isOpen) throw new java.io.IOException("Stream closed") }

-

-  private var haveLeftoverChar: Boolean = false

-  private var leftoverChar: Char = 0

-

-  def getEncoding: String = {

-    if (isOpen) { return encodingName }

-    return null

-  }

-

-  override def read: Int = {

-    return read0

-  }

-

-  // Leftover char is needed because of utf-16 and the surrogate pair stuff.

-  // In that encoding, we need to read two codepoints, and if they are a surrogate pair

-  // synthesize one character from them.

-  //

-  // Presumably this happens in the decoder, but in the case where it isn't a surrogate

-  // pair, we save work by returning two decoded characters, not one since we've already

-  // decoded the second one. (This little optimization seems very not worth it to me, but

-  // is necessary if you can't peek forward 2 bytes into the underlying bytes without 

-  // consuming them.)

-  //

-  // Issue is that UTF-16 is really a variable-width 2-byte or 4-byte character encoding. But 

-  // In the early days of unicode and java, it was treated as a fixed width 2-byte.

-  //

-  // Hence, in DFDL we provide control.

-  // This depends on dfdl:utf16Width="variable". If dfdl:utf16Width="fixed", then 

-  // we do NOT process surrogate pairs, and return them each as a separate codepoint.

-  //

-  // So at some point our decoder must implement utf16Width="fixed" which means it must 

-  // not do this surrogate pair processing. That means either we find a feature for that

-  // in CharsetDecoder (it may be there as this is a common need), or we'll have to clone

-  // and reimplement that class too.

-  //

-  // TBD: implement utf16Width="variable". For now we can SDE on utf16Width="variable".

-  //

-  // This code assumes implicitly that it is ok if the byte stream has been advanced 

-  // further (by one character decoding) than it would have to produce one character only.

-  // 

-  // So long as we're never asking this stream of characters for a byte position we're ok.

-  // 

-  // 

-  // Note: we're not sharing this object across threads. We're assuming one instance per, for

-  // any sort of parallel execution; hence, we don't do lock.synchronized.

-  //

-  private def read0: Int = {

-    //lock.synchronized // in java this was a synchronized method

-    {

-      // Return the leftover char, if there is one

-      if (haveLeftoverChar) {

-        haveLeftoverChar = false

-        return leftoverChar

-      }

-

-      // Convert more bytes

-      val cb: Array[Char] = new Array[Char](2)

-      val n: Int = read(cb, 0, 2)

-      n match {

-        case -1 => return -1

-        case 2 => {

-          leftoverChar = cb(1)

-          haveLeftoverChar = true

-          return cb(0)

-        }

-        case 1 => return cb(0)

-        case _ => {

-          // TODO: assert false : n

-          return -1

-        }

-      }

-    }

-  }

-

-  // All lengths and offsets below are in character units.

-  //

-  def read(cbuf: Array[Char], offset: Int, length: Int): Int = {

-    var off: Int = offset

-    var len: Int = length

-

-    // lock.synchronized // in java this was a synchronized method 

-    {

-      ensureOpen

-      if ((off < 0) || (off > cbuf.length) || (len < 0)

-        || ((off + len) > cbuf.length) || ((off + len) < 0)) {

-        throw new IndexOutOfBoundsException()

-      }

-

-      if (len == 0) { return 0 }

-      var n: Int = 0

-

-      if (haveLeftoverChar) {

-        // Copy the leftover char into the buffer

-        cbuf(off) = leftoverChar

-        off += 1

-        len -= 1

-        haveLeftoverChar = false

-        n = 1

-        if ((len == 0) || !implReady) {

-          // Return now if this is all we can produce w/o blocking

-          DFDLCharCounter.incr(n)

-          return n

-        }

-      }

-

-      // If length is 1, then we recursively end up back here with length 2, 

-      // and that's why it's not a stack overflow.

-      if (len == 1) {

-        // Treat single-character array reads just like read()

-        val c: Int = read0

-        if (c == -1) {

-          return if (n == 0) -1 else {

-            DFDLCharCounter.incr(n)

-            n

-          }

-        }

-        cbuf(off) = c.asInstanceOf[Char]

-        return {

-          DFDLCharCounter.incr(n + 1)

-          n + 1

-        }

-      }

-      val res = n + implRead(cbuf, off, off + len)

-      if (res > 0) DFDLCharCounter.incr(res)

-      return res

-    }

-

-  }

-

-  override def ready: Boolean = {

-    //    lock.synchronized {

-    ensureOpen

-    return haveLeftoverChar || implReady

-    //    }

-  }

-

-  def close: Unit = {

-    //    lock.synchronized {

-    if (!isOpen) { return }

-    implClose

-    isOpen = false

-    //    }

-  }

-

-  private def readBytes: Int = {

-    bb.compact()

-    try {

-      //      if (ch != null) {

-      // Read from the channel

-      val n: Int = ch.read(bb)

-      if (n < 0) return n

-      //      } else {

-      //        // Read from the input stream, and hten update the buffer

-      //        val lim: Int = bb.limit()

-      //        val pos: Int = bb.position()

-      //        assert(pos <= lim)

-      //        val rem: Int = if (pos <= lim) lim - pos else 0

-      //        assert(rem > 0)

-      //        val n: Int = in.read(bb.array, bb.arrayOffset() + pos, rem)

-      //        if (n < 0) return n

-      //        if (n == 0) throw new IOException("Underlying input stream returned zero bytes")

-      //        //assert(n <= rem) : "n = " + n + ", rem = " + rem

-      //        assert(n <= rem)

-      //        bb.position(pos + n)

-      //      }

-    } finally {

-      // Flip even when an IOException is thrown,

-      // otherwise the stream will stutter

-      bb.flip()

-    }

-    val rem: Int = bb.remaining()

-    // assert(rem != 0) : rem

-    assert(rem != 0)

-    return rem

-  }

-

-  /**

-   * Contains change that allows us to treat malformed data as

-   * end of data.  However, this requires that the decoder is set

-   * to REPORT the issue rather than IGNORE or REPLACE it.

-   */

-  def implRead(cbuf: Array[Char], off: Int, end: Int): Int = {

-    // In order to handle surrogate pairs, this method requires that

-    // the invoker attempt to read at least two characters.  Saving the

-    // extra character, if any, at a higher level is easier

-    // to deal with it here.

-    assert(end - off > 1)

-    var cb: CharBuffer = CharBuffer.wrap(cbuf, off, end - off)

-    if (cb.position != 0) {

-      // Ensure that cb[0] == cbuf[off]

-      cb = cb.slice

-    }

-

-    var eof: Boolean = false

-    var continue: Boolean = false

-

-    breakable {

-      while (true) {

-        val cr: CoderResult = decoder.decode(bb, cb, eof) // decode bb to cb

-        if (cr.isUnderflow) { // no more data, or not enough data to complete a character.

-          if (eof) { break } // eof flag set last time around this loop, so if we get an underflow it just reinforces our eof.

-          if (!cb.hasRemaining()) { break } // no more room in cb, so break out. 

-          if ((cb.position() > 0) && !inReady) { break } // we've got at least one character, and we're not ready to read anymore. So break out.

-          val n: Int = readBytes // try to add more bytes to the bb

-          if (n < 0) { // got 0 more bytes

-            eof = true // so we're at EOF.

-            if ((cb.position() == 0) && (!bb.hasRemaining())) { break } // no characters and no room to get more data into bb, then breakout.

-            decoder.reset() // TODO ??? why reset. Can we actually discard any decoder state (such as mid-character)?

-          }

-          continue = true // No continue exists in Scala

-        }

-

-        if (!continue) { // No continue exists in Scala, fabricated one

-          if (cr.isOverflow) {

-            assert(cb.position() > 0)

-            break

-          }

-          Assert.invariant(cr.isMalformed)

-          // DFDL Implementors:

-          // The whole reason we reimplemented this code in scala is

-          // to change this behavior here.

-          //

-          // DFDL needs decode errors to behave as if end of data was reached.

-          // So instead of throw, just set eof true. 

-          // cr.throwException

-          eof = true

-          //

-          // Setting eof to true isn't enough. Because we may have successfully

-          // parsed some characters already, so we'll be returning a count of 

-          // that many.

-          // 

-          // But since we're at EOF we need to return -1 so the calling context

-          // (The addMore member of Page[T] in PagedSeq.scala)

-          // knows we are done and no more can be provided.

-          //

-          // Doc for the CharsetDecoder class says 

-          //     "The malformed bytes begin at the input buffer's (possibly incremented) 

-          //      position"

-          //

-          // So the byte buffer position is left at the end of the correctly decoded

-          // characters.  

-          //

-

-          //

-          // This assert doesn't hold because bb.position() can be zero, but cr.length can be 1 or more. When the very first thing

-          // are malformed, then bb does not get advanced. 

-          //

-          // Similarly, suppose bb.position() was advanced 2 bytes. But then a 4-character

-          // malformed is encountered. So cr.length() is 4. This invariant also will

-          // not hold. 

-          //          Assert.invariant(bb.position() >= cr.length())

-          //          bb.position(bb.position() - cr.length())

-          break

-        }

-        continue = false

-      } // end while loop

-    }

-

-    if (eof) {

-      // ## Need to flush decoder

-      decoder.reset()

-    }

-

-    if (cb.position() == 0) {

-      if (eof) return -1 // note: we have to return -1, having decoded zero characters successfully.

-      assert(false)

-    }

-

-    return cb.position()

-  }

-

-  def encodingName: String = {

-    return if (cs.isInstanceOf[HistoricallyNamedCharset]) cs.asInstanceOf[HistoricallyNamedCharset].historicalName else cs.name()

-  }

-

-  private def inReady: Boolean = {

-    try {

-      return (((in != null) && (in.available() > 0)) || (ch.isInstanceOf[FileChannel])) // ## RBC.available()?

-    } catch {

-      case e: IOException => return false

-    }

-  }

-

-  def implReady: Boolean = { return bb.hasRemaining() || inReady }

-

-  def implClose: Unit = {

-    if (ch != null) ch.close()

-    else in.close()

-  }

-

-}

+ */
+
+import java.nio.CharBuffer
+import java.io.FileInputStream
+import java.nio.charset.Charset
+import sun.nio.cs.HistoricallyNamedCharset
+import java.nio.channels.ReadableByteChannel
+import java.nio.channels.Channels
+import java.io.IOException
+import java.nio.channels.FileChannel
+import java.nio.charset.CharsetDecoder
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import java.nio.charset.CodingErrorAction
+import java.nio.ByteBuffer
+import java.io.InputStream
+import scala.util.control.Breaks._
+import java.nio.charset.CoderResult
+import edu.illinois.ncsa.daffodil.processors.charset.USASCII7BitPackedCharset
+import edu.illinois.ncsa.daffodil.processors.charset.CharsetUtils
+import edu.illinois.ncsa.daffodil.processors.charset.SupportsInitialBitOffset
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import java.nio.charset.MalformedInputException
+import edu.illinois.ncsa.daffodil.processors.charset.CharacterSetAlignmentError
+
+/**
+ * The purpose of re-implementing this class is to gain control over
+ * how the StreamDecoder handles malformed input.  In DFDL we want the
+ * malformed input error to be treated as the end of data.  Java's
+ * StreamDecoder only ignores, replaces or treats it as an error.
+ *
+ * Mostly this class tries to remain true to the Java code from which it was derived
+ * so as to preserve future inter-operation potential. However, some modifications
+ * (for bit-level positioning) make that a lot less likely.
+ */
+object DFDLJavaIOStreamDecoder {
+
+  private val DEFAULT_BYTE_BUFFER_SIZE: Int = Compiler.readerByteBufferSize.toInt
+
+  def forInputStreamReader(in: InputStream, charset: Charset, bitOffset0to7: Int, bitLimit: Long): DFDLJavaIOStreamDecoder = {
+
+    val myBB = ByteBuffer.allocateDirect(DFDLJavaIOStreamDecoder.DEFAULT_BYTE_BUFFER_SIZE)
+    myBB.flip()
+
+    val chan = in match {
+      case fis: FileInputStream => fis.getChannel()
+      case _ => Channels.newChannel(in)
+    }
+
+    new DFDLJavaIOStreamDecoder(bitOffset0to7, bitLimit, charset, myBB, in, chan)
+  }
+
+}
+
+/**
+ * The purpose of re-implementing this class is to gain control over
+ * how the StreamDecoder handles malformed input.  In DFDL we want the
+ * malformed input error to be treated as the end of data.  Java's
+ * StreamDecoder only ignores, replaces or treats it as an error.
+ *
+ * Forces the decoder to REPORT on malformed input.
+ *
+ * We also have to implment the upper-bound aka bitLimit, and not
+ * allow consumption of data past it.
+ */
+class DFDLJavaIOStreamDecoder private (bitOffsetWithinAByte: Int, val bitLimit: Long, var cs: Charset, var bb: ByteBuffer,
+  var in: InputStream, var ch: ReadableByteChannel)
+  extends java.io.Reader {
+
+  Assert.usage(ch != null)
+  Assert.usage(bitOffsetWithinAByte >= 0)
+  Assert.usage(bitOffsetWithinAByte <= 7)
+
+  val decoder = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
+
+  //
+  // Now we deal with bit positioning. If the decoder is capable of dealing with a starting bit offset
+  // then we configure it that way. If not we insist we are byte aligned and fail if not. 
+  //
+  decoder match {
+    case decoderWithBits: SupportsInitialBitOffset =>
+      decoderWithBits.setInitialBitOffset(bitOffsetWithinAByte)
+    case _ if (bitOffsetWithinAByte == 0) => // ok. Do nothing. We are luckily, aligned to a byte.
+    // We're counting on the parser surrounding us to catch this error and turn it into 
+    // a ParseError.
+    case _ => throw new CharacterSetAlignmentError(cs.name, 8, bitOffsetWithinAByte)
+  }
+
+  @volatile
+  private var isOpen: Boolean = true
+  def ensureOpen = { if (!isOpen) throw new java.io.IOException("Stream closed") }
+
+  private var haveLeftoverChar: Boolean = false
+  private var leftoverChar: Char = 0
+
+  def getEncoding: String = {
+    if (isOpen) { return encodingName }
+    return null
+  }
+
+  override def read: Int = {
+    return read0
+  }
+
+  // Leftover char is needed because of utf-16 and the surrogate pair stuff.
+  // In that encoding, we need to read two codepoints, and if they are a surrogate pair
+  // synthesize one character from them.
+  //
+  // Presumably this happens in the decoder, but in the case where it isn't a surrogate
+  // pair, we save work by returning two decoded characters, not one since we've already
+  // decoded the second one. (This little optimization seems very not worth it to me, but
+  // is necessary if you can't peek forward 2 bytes into the underlying bytes without 
+  // consuming them.)
+  //
+  // Issue is that UTF-16 is really a variable-width 2-byte or 4-byte character encoding. But 
+  // In the early days of unicode and java, it was treated as a fixed width 2-byte.
+  //
+  // Hence, in DFDL we provide control.
+  // This depends on dfdl:utf16Width="variable". If dfdl:utf16Width="fixed", then 
+  // we do NOT process surrogate pairs, and return them each as a separate codepoint.
+  //
+  // So at some point our decoder must implement utf16Width="fixed" which means it must 
+  // not do this surrogate pair processing. That means either we find a feature for that
+  // in CharsetDecoder (it may be there as this is a common need), or we'll have to clone
+  // and reimplement that class too.
+  //
+  // TBD: implement utf16Width="variable". For now we can SDE on utf16Width="variable".
+  //
+  // This code assumes implicitly that it is ok if the byte stream has been advanced 
+  // further (by one character decoding) than it would have to produce one character only.
+  // 
+  // So long as we're never asking this stream of characters for a byte position we're ok.
+  // 
+  // 
+  // Note: we're not sharing this object across threads. We're assuming one instance per, for
+  // any sort of parallel execution; hence, we don't do lock.synchronized.
+  //
+  private def read0: Int = {
+    //lock.synchronized // in java this was a synchronized method
+    {
+      // Return the leftover char, if there is one
+      if (haveLeftoverChar) {
+        haveLeftoverChar = false
+        return leftoverChar
+      }
+
+      // Convert more bytes
+      val cb: Array[Char] = new Array[Char](2)
+      val n: Int = read(cb, 0, 2)
+      n match {
+        case -1 => return -1
+        case 2 => {
+          leftoverChar = cb(1)
+          haveLeftoverChar = true
+          return cb(0)
+        }
+        case 1 => return cb(0)
+        case _ => {
+          // TODO: assert false : n
+          return -1
+        }
+      }
+    }
+  }
+
+  // All lengths and offsets below are in character units.
+  //
+  def read(cbuf: Array[Char], offset: Int, length: Int): Int = {
+    var off: Int = offset
+    var len: Int = length
+
+    // lock.synchronized // in java this was a synchronized method 
+    {
+      ensureOpen
+      if ((off < 0) || (off > cbuf.length) || (len < 0)
+        || ((off + len) > cbuf.length) || ((off + len) < 0)) {
+        throw new IndexOutOfBoundsException()
+      }
+
+      if (len == 0) { return 0 }
+      var n: Int = 0
+
+      if (haveLeftoverChar) {
+        // Copy the leftover char into the buffer
+        cbuf(off) = leftoverChar
+        off += 1
+        len -= 1
+        haveLeftoverChar = false
+        n = 1
+        if ((len == 0) || !implReady) {
+          // Return now if this is all we can produce w/o blocking
+          DFDLCharCounter.incr(n)
+          return n
+        }
+      }
+
+      // If length is 1, then we recursively end up back here with length 2, 
+      // and that's why it's not a stack overflow.
+      if (len == 1) {
+        // Treat single-character array reads just like read()
+        val c: Int = read0
+        if (c == -1) {
+          return if (n == 0) -1 else {
+            DFDLCharCounter.incr(n)
+            n
+          }
+        }
+        cbuf(off) = c.asInstanceOf[Char]
+        return {
+          DFDLCharCounter.incr(n + 1)
+          n + 1
+        }
+      }
+      val res = n + implRead(cbuf, off, off + len)
+      if (res > 0) DFDLCharCounter.incr(res)
+      return res
+    }
+
+  }
+
+  override def ready: Boolean = {
+    //    lock.synchronized {
+    ensureOpen
+    return haveLeftoverChar || implReady
+    //    }
+  }
+
+  def close: Unit = {
+    //    lock.synchronized {
+    if (!isOpen) { return }
+    implClose
+    isOpen = false
+    //    }
+  }
+
+  private def readBytes: Int = {
+    bb.compact()
+    try {
+      //      if (ch != null) {
+      // Read from the channel
+      val n: Int = ch.read(bb)
+      if (n < 0) return n
+      //      } else {
+      //        // Read from the input stream, and hten update the buffer
+      //        val lim: Int = bb.limit()
+      //        val pos: Int = bb.position()
+      //        assert(pos <= lim)
+      //        val rem: Int = if (pos <= lim) lim - pos else 0
+      //        assert(rem > 0)
+      //        val n: Int = in.read(bb.array, bb.arrayOffset() + pos, rem)
+      //        if (n < 0) return n
+      //        if (n == 0) throw new IOException("Underlying input stream returned zero bytes")
+      //        //assert(n <= rem) : "n = " + n + ", rem = " + rem
+      //        assert(n <= rem)
+      //        bb.position(pos + n)
+      //      }
+    } finally {
+      // Flip even when an IOException is thrown,
+      // otherwise the stream will stutter
+      bb.flip()
+    }
+    val rem: Int = bb.remaining()
+    // assert(rem != 0) : rem
+    assert(rem != 0)
+    return rem
+  }
+
+  /**
+   * Contains change that allows us to treat malformed data as
+   * end of data.  However, this requires that the decoder is set
+   * to REPORT the issue rather than IGNORE or REPLACE it.
+   */
+  def implRead(cbuf: Array[Char], off: Int, end: Int): Int = {
+    // In order to handle surrogate pairs, this method requires that
+    // the invoker attempt to read at least two characters.  Saving the
+    // extra character, if any, at a higher level is easier
+    // to deal with it here.
+    assert(end - off > 1)
+    var cb: CharBuffer = CharBuffer.wrap(cbuf, off, end - off)
+    if (cb.position != 0) {
+      // Ensure that cb[0] == cbuf[off]
+      cb = cb.slice
+    }
+
+    var eof: Boolean = false
+    var continue: Boolean = false
+
+    breakable {
+      while (true) {
+        val cr: CoderResult = decoder.decode(bb, cb, eof) // decode bb to cb
+        if (cr.isUnderflow) { // no more data, or not enough data to complete a character.
+          if (eof) { break } // eof flag set last time around this loop, so if we get an underflow it just reinforces our eof.
+          if (!cb.hasRemaining()) { break } // no more room in cb, so break out. 
+          if ((cb.position() > 0) && !inReady) { break } // we've got at least one character, and we're not ready to read anymore. So break out.
+          val n: Int = readBytes // try to add more bytes to the bb
+          if (n < 0) { // got 0 more bytes
+            eof = true // so we're at EOF.
+            if ((cb.position() == 0) && (!bb.hasRemaining())) { break } // no characters and no room to get more data into bb, then breakout.
+            decoder.reset() // TODO ??? why reset. Can we actually discard any decoder state (such as mid-character)?
+          }
+          continue = true // No continue exists in Scala
+        }
+
+        if (!continue) { // No continue exists in Scala, fabricated one
+          if (cr.isOverflow) {
+            assert(cb.position() > 0)
+            break
+          }
+          Assert.invariant(cr.isMalformed)
+          // DFDL Implementors:
+          // The whole reason we reimplemented this code in scala is
+          // to change this behavior here.
+          //
+          // DFDL needs decode errors to behave as if end of data was reached.
+          // So instead of throw, just set eof true. 
+          // cr.throwException
+          eof = true
+          //
+          // Setting eof to true isn't enough. Because we may have successfully
+          // parsed some characters already, so we'll be returning a count of 
+          // that many.
+          // 
+          // But since we're at EOF we need to return -1 so the calling context
+          // (The addMore member of Page[T] in PagedSeq.scala)
+          // knows we are done and no more can be provided.
+          //
+          // Doc for the CharsetDecoder class says 
+          //     "The malformed bytes begin at the input buffer's (possibly incremented) 
+          //      position"
+          //
+          // So the byte buffer position is left at the end of the correctly decoded
+          // characters.  
+          //
+
+          //
+          // This assert doesn't hold because bb.position() can be zero, but cr.length can be 1 or more. When the very first thing
+          // are malformed, then bb does not get advanced. 
+          //
+          // Similarly, suppose bb.position() was advanced 2 bytes. But then a 4-character
+          // malformed is encountered. So cr.length() is 4. This invariant also will
+          // not hold. 
+          //          Assert.invariant(bb.position() >= cr.length())
+          //          bb.position(bb.position() - cr.length())
+          break
+        }
+        continue = false
+      } // end while loop
+    }
+
+    if (eof) {
+      // ## Need to flush decoder
+      decoder.reset()
+    }
+
+    if (cb.position() == 0) {
+      if (eof) return -1 // note: we have to return -1, having decoded zero characters successfully.
+      assert(false)
+    }
+
+    return cb.position()
+  }
+
+  def encodingName: String = {
+    return if (cs.isInstanceOf[HistoricallyNamedCharset]) cs.asInstanceOf[HistoricallyNamedCharset].historicalName else cs.name()
+  }
+
+  private def inReady: Boolean = {
+    try {
+      return (((in != null) && (in.available() > 0)) || (ch.isInstanceOf[FileChannel])) // ## RBC.available()?
+    } catch {
+      case e: IOException => return false
+    }
+  }
+
+  def implReady: Boolean = { return bb.hasRemaining() || inReady }
+
+  def implClose: Unit = {
+    if (ch != null) ch.close()
+    else in.close()
+  }
+
+}
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLReader.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLReader.scala
index 95d6c15..d0a99db 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLReader.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLReader.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.processors

-

+package edu.illinois.ncsa.daffodil.processors
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,385 +30,385 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import java.io.InputStream

-import java.nio.channels.ReadableByteChannel

-import scala.collection.immutable.PagedSeq

-import scala.collection.mutable.HashMap

-import scala.util.parsing.input.OffsetPosition

-import java.nio.charset.CodingErrorAction

-import java.nio.ByteBuffer

-import java.io.InputStreamReader

-import sun.nio.cs.StreamDecoder

-import java.nio.charset.Charset

-import java.io.UnsupportedEncodingException

-import java.nio.charset.IllegalCharsetNameException

-import java.nio.charset.CharsetDecoder

-import java.io.FileInputStream

-import java.nio.channels.FileChannel

-import java.io.IOException

-import java.nio.CharBuffer

-import scala.util.control.Breaks._

-import java.nio.charset.CoderResult

-import sun.nio.cs.HistoricallyNamedCharset

-import java.nio.channels.Channels

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import scala.util.parsing.input.Reader

-import scala.util.parsing.input.CharSequenceReader

-import edu.illinois.ncsa.daffodil.util._

-

-//

-// Convention: name index fields like bytePos or bitPos or charPos with suffixes to indicate

-// zero based or 1 based. Suffixes are ...0b and ...1b respectively.

-//

-

-/**

- * Pure functional Reader[Byte] that gets its data from a DFDL.Input (aka a ReadableByteChannel)

- *

- * All reading of data ultimately comes to this layer which retrieves data on demand.

- *

- * This layer doesn't know anything about bits and bit positions like all the higher layers do.

- */

-class DFDLByteReader private (psb: PagedSeq[Byte], val bytePos0b: Int = 0)

-  extends scala.util.parsing.input.Reader[Byte] with Logging {

-

-  def this(in: ReadableByteChannel) = this(PagedSeq.fromIterator(new IterableReadableByteChannel(in)), 0)

-

-  /**

-   * Note: calling this will force the entire input into memory.

-   */

-  def lengthInBytes: Long = psb.length

-

-  lazy val first: Byte = psb(bytePos0b)

-

-  lazy val rest: DFDLByteReader = new DFDLByteReader(psb, bytePos0b + 1)

-

-  // needed because of contract from Reader superclass.

-  lazy val pos: scala.util.parsing.input.Position = new DFDLBytePosition(bytePos0b)

-

-  lazy val atEnd: Boolean = !psb.isDefinedAt(bytePos0b)

-

-  def atPos(bytePosition0b: Int): DFDLByteReader = {

-    // note: do NOT slice. That copies the psb.

-    //new DFDLByteReader(psb.slice(bytePosition), 0) 

-    if (bytePosition0b == bytePos0b) this // already at this position.

-    else new DFDLByteReader(psb, bytePosition0b)

-  }

-

-  def getByte(bytePosition0b: Int): Byte = {

-    val res = psb(bytePosition0b)

-    res

-  }

-

-  def getByteArray(bytePosition0b: Int, numBytes: Int): Array[Byte] = {

-    val arr = new Array[Byte](numBytes)

-    for (i <- 0 to (numBytes - 1)) {

-      arr(i) = getByte(bytePosition0b + i)

-    }

-    arr

-  }

-

-  // Removed: These are a really bad idea. They will make a giant 

-  // copy of the entire input. Maybe ok for unit testing, but just

-  // having them here is asking for trouble.

-  //

-  // lazy val byteArray: Array[Byte] = psb.toArray[Byte]

-  // lazy val bb: ByteBuffer = ByteBuffer.wrap(byteArray)

-

-  /**

-   * Factory for a Reader[Char] that constructs characters by decoding them from this

-   * Reader[Byte] for a specific encoding starting at a particular bit position.

-   *

-   * Yes, I said "bit" position. Some characters are not a full byte wide (7-bit, 6-bit, and even 5-bit

-   * encodings exist)

-   *

-   * These are kept in the processor state for reuse.

-   */

-  def newCharReader(charset: Charset, bitPos: Long, bitLimit: Long): DFDLCharReader = {

-    log(LogLevel.Debug, "DFDLByteReader.newCharReader for bytePos %s.", (bitPos >> 3))

-    DFDLCharReader(psb, bitPos, bitLimit, charset)

-  }

-

-}

-

-object DFDLCharReader {

-

-  // TODO: make a specialized DFDLSingleByteCharReader for known single-byte character sets.

-  // This can bypass the PagedSeq[Char] entirely.

-  def apply(thePsb: PagedSeq[Byte], bitPosition: Long, bitLimit: Long, charset: Charset): DFDLCharReader = {

-

-    Assert.usage(bitPosition <= Int.MaxValue, "bit positions are limited to 32-bit signed integer by underlying libraries.")

-    val bitPos = bitPosition.toInt

-

-    val bitOffset = bitPos & 0x7

-    val bytePos = bitPos >> 3

-

-    val is = {

-      // 

-      // Removed slice call: psb.slice makes a copy of the psb.

-      // now passes the psb and start/end to the IteratorInputStream

-      // which manages delivery of bytes one by one so as to not 

-      // do any copying that isn't necessary.

-      //

-      val endBytePos =

-        if (bitLimit == -1) -1

-        // Here we want to limit the PagedSeq[Byte] via bitLimit

-        // because we need to determine the ending byte position from

-        // the bit limit we must divide by 8.0 (must divide by double)

-        // in order to round to the appropriate byte position

-        else scala.math.ceil(bitLimit / 8.0).toInt

-      new IteratorInputStream(thePsb, bytePos, endBytePos)

-    }

-

-    // TODO: Why is bitLimit not working for DFDLJavaIOInputStreamReader?

-    // it appears to not be implemented, why is it there at all?

-    val r = DFDLJavaIOInputStreamReader(is, charset, bitOffset, bitLimit)

-    // TRW - The following line was changed because the fromSource

-    // method was causing the readLine method of the BufferedReader class to be

-    // called.  This resulted in the loss of \n, \r and \r\n characters from the data.

-    //val psc = PagedSeq.fromSource(scala.io.Source.fromInputStream(is)(codec))

-    val psc = PagedSeq.fromReader(r)

-    val charOffset = 0

-    // val rdr = new DFDLPagedSeqCharReader(charset, bitOffset, bitLimit, psc, charOffset, thePsb)

-    val rdr = new DFDLPagedSeqCharReader(charset, bitPos, bitLimit, psc, charOffset, thePsb)

-    rdr

-  }

-

-}

-

-/**

- * Reader[Char] constructed from a specific point within a PagedSeq[Byte], for

- * a particular character set encoding. Ends if there is any error trying to decode a

- * character.

- *

- * This trait allows for multiple different implementations for performance

- * reasons.

- *

- * Some implementations deal with the general issue of variable-width

- * character encodings.

- *

- * Others are specialized for 1-to-1 single-byte character encodings

- * like US-ASCII or ISO-8859-1, where the mapping to unicode characters

- * is either trivial, or requires just a small lookup table.

- */

-

-trait DFDLCharReader

-  extends Reader[Char] {

-  def first: Char

-  def rest: DFDLCharReader

-  def atEnd: Boolean

-  def atCharPos(cp0b: Int): DFDLCharReader

-  def atBitPos(bp0b: Long): DFDLCharReader

-  def getCharsetName: String

-  def characterPos: Int

-  def charset: Charset

-  def bitLimit: Long

-}

-

-/**

- * This is for unit tests that want to feed data from a string

- */

-class DFDLUTStringReader private (rdr: Reader[Char])

-  extends DFDLCharReader {

-  override def source = rdr.source

-  override def offset = rdr.offset

-  def this(data: String) = this(new CharSequenceReader(data))

-  def first = rdr.first

-  def rest = new DFDLUTStringReader(rdr.rest)

-  def atEnd = rdr.atEnd

-  def pos = rdr.pos

-  def atCharPos(cp0b: Int) = Assert.usageError("not to be used in test reader")

-  def atBitPos(bp0b: Long) = Assert.usageError("not to be used in test reader")

-  def getCharsetName = Assert.usageError("not to be used in test reader")

-  def characterPos = Assert.usageError("not to be used in test reader")

-  def charset = Assert.usageError("not to be used in test reader")

-  def bitLimit = -1

-}

-

-// TODO: make this global singleton go away!

-// This state should be maintained in the DataProcessor object I think.

-object DFDLCharCounter {

-  var count: Long = 0

-  def incr(n: Long) {

-    count += n

-  }

-  def getAndResetCount = {

-    val c = count

-    count = 0

-    c

-  }

-}

-/**

- * This is for arbitrary character sets. Uses a PagedSeq[Char] as underlying cache.

- */

-class DFDLPagedSeqCharReader(charsetArg: Charset,

-  val startingBitPos: Int,

-  bitLimitArg: Long,

-  psc: PagedSeq[Char],

-  override val offset: Int,

-  psb: PagedSeq[Byte])

-  extends DFDLCharReader with Logging {

-

-  Assert.usage(offset >= 0)

-  Assert.usage(startingBitPos >= 0)

-

-  val charset = charsetArg

-  val bitLimit = bitLimitArg

-

-  override lazy val source: CharSequence = psc

-

-  def first: Char = {

-    val char = psc(offset)

-    char

-  }

-

-  def rest: DFDLCharReader =

-    if (psc.isDefinedAt(offset)) new DFDLPagedSeqCharReader(charset, startingBitPos, bitLimit, psc, offset + 1, psb)

-    else this

-

-  def atEnd: Boolean = !psc.isDefinedAt(offset)

-

-  def pos: scala.util.parsing.input.Position = new OffsetPosition(source, offset) //new DFDLCharPosition(offset)

-

-  override def drop(n: Int): DFDLCharReader = new DFDLPagedSeqCharReader(charset, startingBitPos, bitLimit, psc, offset + n, psb)

-

-  def atCharPos(characterPos: Int): DFDLCharReader = {

-    if (characterPos == this.characterPos) this

-    else new DFDLPagedSeqCharReader(charset, startingBitPos, bitLimit, psc, characterPos, psb)

-  }

-

-  // We really want to be able to ask for a CharReader starting at said bitPos

-  def atBitPos(bitPos: Long): DFDLCharReader = {

-    log(LogLevel.Debug, "creating new DFDLCharReader.atBytePos(%s)", (bitPos >> 3))

-    new DFDLPagedSeqCharReader(charset, startingBitPos = bitPos.toInt, bitLimit, psc, characterPos, psb)

-  }

-

-  def getCharsetName: String = charset.name()

-

-  def characterPos: Int = offset

-

-  // def isDefinedAt(charPos : Int) : Boolean = psc.isDefinedAt(charPos)

-

-  def print: String = {

-    "DFDLCharReader - " + source.length() + ": " + source + "\nDFDLCharReader - " + characterPos + ": " + source.subSequence(characterPos, source.length())

-  }

-

-  override def toString = {

-    "DFDLCharReader starting at bitPos " + startingBitPos + " charPos " + characterPos + " bitLimit " + bitLimit

-  }

-

-}

-

-// Scala Reader stuff is not consistent about whether it is generic over the element type, 

-// or specific to Char. We want to have a Reader like abstraction that is over bytes, but 

-// be able to create real Reader[Char] from it at any byte position.

-

-object IterableReadableByteChannel {

-  var byteCount: Long = 0

-  def getAndResetCalls = {

-    val res = byteCount

-    byteCount = 0

-    res

-  }

-}

-

-/**

- * All this excess buffering layer for lack of a way to convert a ReadableByteChannel directly into

- * a PagedSeq. We need an Iterator[Byte] first to construct a PagedSeq[Byte].

- */

-class IterableReadableByteChannel(rbc: ReadableByteChannel)

-  extends scala.collection.Iterator[Byte] {

-

-  private final val bufferSize = 10000

-  private var currentBuf: java.nio.ByteBuffer = _

-  private var sz: Int = _

-

-  private def advanceToNextBuf() {

-    currentBuf = java.nio.ByteBuffer.allocate(bufferSize)

-    sz = rbc.read(currentBuf)

-    currentBuf.flip()

-  }

-

-  advanceToNextBuf()

-

-  def hasNext(): Boolean = {

-    if (sz == -1) return false

-    if (currentBuf.hasRemaining()) return true

-    advanceToNextBuf()

-    if (sz == -1) return false

-    if (currentBuf.hasRemaining()) return true

-    return false

-  }

-

-  var pos: Int = 0

-

-  def next(): Byte = {

-    if (!hasNext()) throw new IndexOutOfBoundsException(pos.toString)

-    pos += 1

-    IterableReadableByteChannel.byteCount += 1

-    currentBuf.get()

-  }

-}

-

-/**

- * Scala's Position is document oriented in that it is 1-based indexing and assumes

- * line numbers and column numbers.

- *

- */

-class DFDLBytePosition(i: Int) extends scala.util.parsing.input.Position {

-  def line = 1

-  def column = i + 1

-  // IDEA: could we assume a 'line' of bytes is 32 bytes because those print out nicely as 

-  // as in HHHHHHHH HHHHHHHH ... etc. on a 72 character line?

-  // Could come in handy perhaps. 

-  val lineContents = "" // unused. Maybe this should throw. NoSuchOperation, or something.

-}

-

-/**

- * Position in a character stream.

- *

- * We ignore line/column structure. It's all one "line" as far as we are concerned.

- */

-class DFDLCharPosition(i: Int) extends scala.util.parsing.input.Position {

-  def line = 1

-  def column = i + 1

-  val lineContents = "" // unused

-}

-

-/**

- * Whole additional layer of byte-by-byte because there's no way to create

- * a Source (of Char) from a Seq[Byte]. Instead we have to take our

- * PagedSeq[Byte] to an Iterator, create an InputStream from the Iterator,

- * and create a Source (of Char) from that.

- *

- * Convert an iterator of bytes into an InputStream

- */

-

-object IteratorInputStream {

-  var calls: Long = 0 // instrumentation for performance analysis.

-  def getAndResetCalls: Long = {

-    val c = calls

-    calls = 0

-    c

-  }

-}

-

-class IteratorInputStream(psb: PagedSeq[Byte], startBytePos0b: Int, endBytePos0b: Int)

-  extends InputStream with Logging {

-

-  log(LogLevel.Debug, "Creating an IteratorInputStream. This should happen only once per DataProcessor.parse call")

-  var currentBytePos0b: Int = startBytePos0b

-

-  def read(): Int = {

-    if (currentBytePos0b == endBytePos0b

-      || !psb.isDefinedAt(currentBytePos0b)) -1

-    else {

-      IteratorInputStream.calls += 1

-      val res = psb(currentBytePos0b)

-      currentBytePos0b += 1

-      res

-    }

-  }

-

-}

+ */
+
+import java.io.InputStream
+import java.nio.channels.ReadableByteChannel
+import scala.collection.immutable.PagedSeq
+import scala.collection.mutable.HashMap
+import scala.util.parsing.input.OffsetPosition
+import java.nio.charset.CodingErrorAction
+import java.nio.ByteBuffer
+import java.io.InputStreamReader
+import sun.nio.cs.StreamDecoder
+import java.nio.charset.Charset
+import java.io.UnsupportedEncodingException
+import java.nio.charset.IllegalCharsetNameException
+import java.nio.charset.CharsetDecoder
+import java.io.FileInputStream
+import java.nio.channels.FileChannel
+import java.io.IOException
+import java.nio.CharBuffer
+import scala.util.control.Breaks._
+import java.nio.charset.CoderResult
+import sun.nio.cs.HistoricallyNamedCharset
+import java.nio.channels.Channels
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import scala.util.parsing.input.Reader
+import scala.util.parsing.input.CharSequenceReader
+import edu.illinois.ncsa.daffodil.util._
+
+//
+// Convention: name index fields like bytePos or bitPos or charPos with suffixes to indicate
+// zero based or 1 based. Suffixes are ...0b and ...1b respectively.
+//
+
+/**
+ * Pure functional Reader[Byte] that gets its data from a DFDL.Input (aka a ReadableByteChannel)
+ *
+ * All reading of data ultimately comes to this layer which retrieves data on demand.
+ *
+ * This layer doesn't know anything about bits and bit positions like all the higher layers do.
+ */
+class DFDLByteReader private (psb: PagedSeq[Byte], val bytePos0b: Int = 0)
+  extends scala.util.parsing.input.Reader[Byte] with Logging {
+
+  def this(in: ReadableByteChannel) = this(PagedSeq.fromIterator(new IterableReadableByteChannel(in)), 0)
+
+  /**
+   * Note: calling this will force the entire input into memory.
+   */
+  def lengthInBytes: Long = psb.length
+
+  lazy val first: Byte = psb(bytePos0b)
+
+  lazy val rest: DFDLByteReader = new DFDLByteReader(psb, bytePos0b + 1)
+
+  // needed because of contract from Reader superclass.
+  lazy val pos: scala.util.parsing.input.Position = new DFDLBytePosition(bytePos0b)
+
+  lazy val atEnd: Boolean = !psb.isDefinedAt(bytePos0b)
+
+  def atPos(bytePosition0b: Int): DFDLByteReader = {
+    // note: do NOT slice. That copies the psb.
+    //new DFDLByteReader(psb.slice(bytePosition), 0) 
+    if (bytePosition0b == bytePos0b) this // already at this position.
+    else new DFDLByteReader(psb, bytePosition0b)
+  }
+
+  def getByte(bytePosition0b: Int): Byte = {
+    val res = psb(bytePosition0b)
+    res
+  }
+
+  def getByteArray(bytePosition0b: Int, numBytes: Int): Array[Byte] = {
+    val arr = new Array[Byte](numBytes)
+    for (i <- 0 to (numBytes - 1)) {
+      arr(i) = getByte(bytePosition0b + i)
+    }
+    arr
+  }
+
+  // Removed: These are a really bad idea. They will make a giant 
+  // copy of the entire input. Maybe ok for unit testing, but just
+  // having them here is asking for trouble.
+  //
+  // lazy val byteArray: Array[Byte] = psb.toArray[Byte]
+  // lazy val bb: ByteBuffer = ByteBuffer.wrap(byteArray)
+
+  /**
+   * Factory for a Reader[Char] that constructs characters by decoding them from this
+   * Reader[Byte] for a specific encoding starting at a particular bit position.
+   *
+   * Yes, I said "bit" position. Some characters are not a full byte wide (7-bit, 6-bit, and even 5-bit
+   * encodings exist)
+   *
+   * These are kept in the processor state for reuse.
+   */
+  def newCharReader(charset: Charset, bitPos: Long, bitLimit: Long): DFDLCharReader = {
+    log(LogLevel.Debug, "DFDLByteReader.newCharReader for bytePos %s.", (bitPos >> 3))
+    DFDLCharReader(psb, bitPos, bitLimit, charset)
+  }
+
+}
+
+object DFDLCharReader {
+
+  // TODO: make a specialized DFDLSingleByteCharReader for known single-byte character sets.
+  // This can bypass the PagedSeq[Char] entirely.
+  def apply(thePsb: PagedSeq[Byte], bitPosition: Long, bitLimit: Long, charset: Charset): DFDLCharReader = {
+
+    Assert.usage(bitPosition <= Int.MaxValue, "bit positions are limited to 32-bit signed integer by underlying libraries.")
+    val bitPos = bitPosition.toInt
+
+    val bitOffset = bitPos & 0x7
+    val bytePos = bitPos >> 3
+
+    val is = {
+      // 
+      // Removed slice call: psb.slice makes a copy of the psb.
+      // now passes the psb and start/end to the IteratorInputStream
+      // which manages delivery of bytes one by one so as to not 
+      // do any copying that isn't necessary.
+      //
+      val endBytePos =
+        if (bitLimit == -1) -1
+        // Here we want to limit the PagedSeq[Byte] via bitLimit
+        // because we need to determine the ending byte position from
+        // the bit limit we must divide by 8.0 (must divide by double)
+        // in order to round to the appropriate byte position
+        else scala.math.ceil(bitLimit / 8.0).toInt
+      new IteratorInputStream(thePsb, bytePos, endBytePos)
+    }
+
+    // TODO: Why is bitLimit not working for DFDLJavaIOInputStreamReader?
+    // it appears to not be implemented, why is it there at all?
+    val r = DFDLJavaIOInputStreamReader(is, charset, bitOffset, bitLimit)
+    // TRW - The following line was changed because the fromSource
+    // method was causing the readLine method of the BufferedReader class to be
+    // called.  This resulted in the loss of \n, \r and \r\n characters from the data.
+    //val psc = PagedSeq.fromSource(scala.io.Source.fromInputStream(is)(codec))
+    val psc = PagedSeq.fromReader(r)
+    val charOffset = 0
+    // val rdr = new DFDLPagedSeqCharReader(charset, bitOffset, bitLimit, psc, charOffset, thePsb)
+    val rdr = new DFDLPagedSeqCharReader(charset, bitPos, bitLimit, psc, charOffset, thePsb)
+    rdr
+  }
+
+}
+
+/**
+ * Reader[Char] constructed from a specific point within a PagedSeq[Byte], for
+ * a particular character set encoding. Ends if there is any error trying to decode a
+ * character.
+ *
+ * This trait allows for multiple different implementations for performance
+ * reasons.
+ *
+ * Some implementations deal with the general issue of variable-width
+ * character encodings.
+ *
+ * Others are specialized for 1-to-1 single-byte character encodings
+ * like US-ASCII or ISO-8859-1, where the mapping to unicode characters
+ * is either trivial, or requires just a small lookup table.
+ */
+
+trait DFDLCharReader
+  extends Reader[Char] {
+  def first: Char
+  def rest: DFDLCharReader
+  def atEnd: Boolean
+  def atCharPos(cp0b: Int): DFDLCharReader
+  def atBitPos(bp0b: Long): DFDLCharReader
+  def getCharsetName: String
+  def characterPos: Int
+  def charset: Charset
+  def bitLimit: Long
+}
+
+/**
+ * This is for unit tests that want to feed data from a string
+ */
+class DFDLUTStringReader private (rdr: Reader[Char])
+  extends DFDLCharReader {
+  override def source = rdr.source
+  override def offset = rdr.offset
+  def this(data: String) = this(new CharSequenceReader(data))
+  def first = rdr.first
+  def rest = new DFDLUTStringReader(rdr.rest)
+  def atEnd = rdr.atEnd
+  def pos = rdr.pos
+  def atCharPos(cp0b: Int) = Assert.usageError("not to be used in test reader")
+  def atBitPos(bp0b: Long) = Assert.usageError("not to be used in test reader")
+  def getCharsetName = Assert.usageError("not to be used in test reader")
+  def characterPos = Assert.usageError("not to be used in test reader")
+  def charset = Assert.usageError("not to be used in test reader")
+  def bitLimit = -1
+}
+
+// TODO: make this global singleton go away!
+// This state should be maintained in the DataProcessor object I think.
+object DFDLCharCounter {
+  var count: Long = 0
+  def incr(n: Long) {
+    count += n
+  }
+  def getAndResetCount = {
+    val c = count
+    count = 0
+    c
+  }
+}
+/**
+ * This is for arbitrary character sets. Uses a PagedSeq[Char] as underlying cache.
+ */
+class DFDLPagedSeqCharReader(charsetArg: Charset,
+  val startingBitPos: Int,
+  bitLimitArg: Long,
+  psc: PagedSeq[Char],
+  override val offset: Int,
+  psb: PagedSeq[Byte])
+  extends DFDLCharReader with Logging {
+
+  Assert.usage(offset >= 0)
+  Assert.usage(startingBitPos >= 0)
+
+  val charset = charsetArg
+  val bitLimit = bitLimitArg
+
+  override lazy val source: CharSequence = psc
+
+  def first: Char = {
+    val char = psc(offset)
+    char
+  }
+
+  def rest: DFDLCharReader =
+    if (psc.isDefinedAt(offset)) new DFDLPagedSeqCharReader(charset, startingBitPos, bitLimit, psc, offset + 1, psb)
+    else this
+
+  def atEnd: Boolean = !psc.isDefinedAt(offset)
+
+  def pos: scala.util.parsing.input.Position = new OffsetPosition(source, offset) //new DFDLCharPosition(offset)
+
+  override def drop(n: Int): DFDLCharReader = new DFDLPagedSeqCharReader(charset, startingBitPos, bitLimit, psc, offset + n, psb)
+
+  def atCharPos(characterPos: Int): DFDLCharReader = {
+    if (characterPos == this.characterPos) this
+    else new DFDLPagedSeqCharReader(charset, startingBitPos, bitLimit, psc, characterPos, psb)
+  }
+
+  // We really want to be able to ask for a CharReader starting at said bitPos
+  def atBitPos(bitPos: Long): DFDLCharReader = {
+    log(LogLevel.Debug, "creating new DFDLCharReader.atBytePos(%s)", (bitPos >> 3))
+    new DFDLPagedSeqCharReader(charset, startingBitPos = bitPos.toInt, bitLimit, psc, characterPos, psb)
+  }
+
+  def getCharsetName: String = charset.name()
+
+  def characterPos: Int = offset
+
+  // def isDefinedAt(charPos : Int) : Boolean = psc.isDefinedAt(charPos)
+
+  def print: String = {
+    "DFDLCharReader - " + source.length() + ": " + source + "\nDFDLCharReader - " + characterPos + ": " + source.subSequence(characterPos, source.length())
+  }
+
+  override def toString = {
+    "DFDLCharReader starting at bitPos " + startingBitPos + " charPos " + characterPos + " bitLimit " + bitLimit
+  }
+
+}
+
+// Scala Reader stuff is not consistent about whether it is generic over the element type, 
+// or specific to Char. We want to have a Reader like abstraction that is over bytes, but 
+// be able to create real Reader[Char] from it at any byte position.
+
+object IterableReadableByteChannel {
+  var byteCount: Long = 0
+  def getAndResetCalls = {
+    val res = byteCount
+    byteCount = 0
+    res
+  }
+}
+
+/**
+ * All this excess buffering layer for lack of a way to convert a ReadableByteChannel directly into
+ * a PagedSeq. We need an Iterator[Byte] first to construct a PagedSeq[Byte].
+ */
+class IterableReadableByteChannel(rbc: ReadableByteChannel)
+  extends scala.collection.Iterator[Byte] {
+
+  private final val bufferSize = 10000
+  private var currentBuf: java.nio.ByteBuffer = _
+  private var sz: Int = _
+
+  private def advanceToNextBuf() {
+    currentBuf = java.nio.ByteBuffer.allocate(bufferSize)
+    sz = rbc.read(currentBuf)
+    currentBuf.flip()
+  }
+
+  advanceToNextBuf()
+
+  def hasNext(): Boolean = {
+    if (sz == -1) return false
+    if (currentBuf.hasRemaining()) return true
+    advanceToNextBuf()
+    if (sz == -1) return false
+    if (currentBuf.hasRemaining()) return true
+    return false
+  }
+
+  var pos: Int = 0
+
+  def next(): Byte = {
+    if (!hasNext()) throw new IndexOutOfBoundsException(pos.toString)
+    pos += 1
+    IterableReadableByteChannel.byteCount += 1
+    currentBuf.get()
+  }
+}
+
+/**
+ * Scala's Position is document oriented in that it is 1-based indexing and assumes
+ * line numbers and column numbers.
+ *
+ */
+class DFDLBytePosition(i: Int) extends scala.util.parsing.input.Position {
+  def line = 1
+  def column = i + 1
+  // IDEA: could we assume a 'line' of bytes is 32 bytes because those print out nicely as 
+  // as in HHHHHHHH HHHHHHHH ... etc. on a 72 character line?
+  // Could come in handy perhaps. 
+  val lineContents = "" // unused. Maybe this should throw. NoSuchOperation, or something.
+}
+
+/**
+ * Position in a character stream.
+ *
+ * We ignore line/column structure. It's all one "line" as far as we are concerned.
+ */
+class DFDLCharPosition(i: Int) extends scala.util.parsing.input.Position {
+  def line = 1
+  def column = i + 1
+  val lineContents = "" // unused
+}
+
+/**
+ * Whole additional layer of byte-by-byte because there's no way to create
+ * a Source (of Char) from a Seq[Byte]. Instead we have to take our
+ * PagedSeq[Byte] to an Iterator, create an InputStream from the Iterator,
+ * and create a Source (of Char) from that.
+ *
+ * Convert an iterator of bytes into an InputStream
+ */
+
+object IteratorInputStream {
+  var calls: Long = 0 // instrumentation for performance analysis.
+  def getAndResetCalls: Long = {
+    val c = calls
+    calls = 0
+    c
+  }
+}
+
+class IteratorInputStream(psb: PagedSeq[Byte], startBytePos0b: Int, endBytePos0b: Int)
+  extends InputStream with Logging {
+
+  log(LogLevel.Debug, "Creating an IteratorInputStream. This should happen only once per DataProcessor.parse call")
+  var currentBytePos0b: Int = startBytePos0b
+
+  def read(): Int = {
+    if (currentBytePos0b == endBytePos0b
+      || !psb.isDefinedAt(currentBytePos0b)) -1
+    else {
+      IteratorInputStream.calls += 1
+      val res = psb(currentBytePos0b)
+      currentBytePos0b += 1
+      res
+    }
+  }
+
+}
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLRegularExpressions.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLRegularExpressions.scala
index 7ffe69d..fac6f99 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLRegularExpressions.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/DFDLRegularExpressions.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.processors

+package edu.illinois.ncsa.daffodil.processors
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,146 +32,146 @@
  * SOFTWARE.
  */
 
-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification

-

-object DFDLRegularExpressions {

-

-  def getEscapeRegEx(escape: String, escapeEscape: String, delim: String,

-                     dotMatchesAll: Boolean = true) = {

-    val str =

-      (if (dotMatchesAll) """(?s)""" else """""") +

-        """(.*?)""" + // content [before]. lazy so it won't absorb pad characters

-        """(?:(?<!(?:(?<!%1$s)%2$s))(%3$s))""" + // unescaped delimiter [delim]

-        """(.*)""" // trailing stuff [after]

-    val contentPattern = str.format(escapeEscape, escape, delim).r

-    contentPattern

-  }

-

-  def getEscapeRegExWithPadding(escape: String, escapeEscape: String, delim: String,

-                                padChar: String, justification: TextStringJustification,

-                                dotMatchesAll: Boolean = true) = {

-    val str =

-      (if (dotMatchesAll) """(?s)""" else """""") +

-        (if (justification == TextStringJustification.Right

-          || justification == TextStringJustification.Center)

-          """(?:%4$s*)""" // trim pad char. aka right or center justified.

-        else """""") +

-        """(.*?)""" + // content [before]. lazy so it won't absorb pad characters

-        (if (justification == TextStringJustification.Left

-          || justification == TextStringJustification.Center)

-          """(?:%4$s*)""" // trim pad char. aka left or center justified.

-        else """""") +

-        """(?:(?<!(?:(?<!%1$s)%2$s))(%3$s))""" + // unescaped delimiter [delim]

-        """(.*)""" // trailing stuff [after]

-    val contentPattern = str.format(escapeEscape, escape, delim, padChar).r

-    contentPattern

-  }

-

-  def getSameEscapeRegEx(escape: String, delim: String,

-                         dotMatchesAll: Boolean = true) = {

-    val str =

-      (if (dotMatchesAll) """(?s)""" else """""") +

-        """(.*?)""" + // content [before]. lazy so it won't absorb pad characters

-        """(?:""" +

-        """(?<!%1$s)((?:%1$s%1$s)*)(%2$s)""" + // unescaped delimiter [delim] which is delim preceded by NOT an odd number of escapes.

-        """)""" +

-        """(.*)""" // trailing stuff [after]

-    val contentPattern = str.format(escape, delim).r

-    contentPattern

-  }

-

-  def getSameEscapeRegExWithPadding(escape: String, delim: String,

-                                    padChar: String, justification: TextStringJustification,

-                                    dotMatchesAll: Boolean = true) = {

-    val str =

-      (if (dotMatchesAll) """(?s)""" else """""") +

-        (if (justification == TextStringJustification.Right

-          || justification == TextStringJustification.Center)

-          """(?<!%1$s)((?:%1$s%1$s)*)(?:%3$s*)""" // trim unescaped pad char. aka right or center justified.

-        else """""") +

-        """(.*?)""" + // content [before]. lazy so it won't absorb pad characters

-        """(?:""" +

-        (if (justification == TextStringJustification.Left

-          || justification == TextStringJustification.Center)

-          """(?<!%1$s)((?:%1$s%1$s)*)(?:%3$s*)(?<!%1$s)(%2$s)""" + // trim unescaped pad char. aka left or center justified. Then delim. (and if pad is size zero, then delimiter must be unescaped.)

-          """|""" // OR

-        else """""") +

-        """(?<!%1$s)((?:%1$s%1$s)*)(%2$s)""" + // unescaped delimiter [delim] which is delim preceded by NOT an odd number of escapes.

-        """)""" +

-        """(.*)""" // trailing stuff [after]

-    val contentPattern = str.format(escape, delim, padChar).r

-    //System.err.println(contentPattern)

-    contentPattern

-  }

-

-  def getEscapeBlockRegEx(bStart: String, bEnd: String, escapeEscape: String,

-                          escape: String, delim: String, dotMatchesAll: Boolean = true) = {

-    // Based upon assumption that String.format ignores extra arguments for which there

-    // are no specifiers

-    //

-    val str =

-      (if (dotMatchesAll) """(?s)""" else """""") +

-        """(?>""" +

-        // First alternative is if the start is an unescaped Block start. In that case you must

-        // have an unescaped block end followed directly by an unescaped delimiter as the termination.

-        """(?<!(?<!%1$s)%2$s)%4$s""" + // unescaped block start

-        """(.*)""" + // captured content (before)

-        """(?<!(?<!%1$s)%2$s)%5$s""" + // unescaped block end

-        """(?<!(?<!%1$s)%2$s)(%3$s)""" + // unescaped delimiter (delim)

-        """(.*)""" + // trailing stuff (after)

-        """|""" +

-        // Second alternative is if the start is NOT a block start, in which case we are looking 

-        // for an unescaped delimiter at the end.

-        """(?<!%4$s)""" + // not a block start

-        """(.*?)""" + // lazy content so it won't absorb the padChars (before2)

-        """(?<!(?<!%1$s)%2$s)(%3$s)""" + // unescaped delimiter (delim2)

-        """(.*)""" + // trailing stuff (after2)

-        """)"""

-    val contentPattern = str.format(escapeEscape, escape, delim, bStart, bEnd).r

-    contentPattern

-  }

-

-  def getEscapeBlockRegExWithPadding(bStart: String, bEnd: String, escapeEscape: String,

-                                     escape: String, padChar: String, delim: String, justification: TextStringJustification,

-                                     dotMatchesAll: Boolean = true) = {

-    // Based upon assumption that String.format ignores extra arguments for which there

-    // are no specifiers

-    //

-    val str =

-      (if (dotMatchesAll) """(?s)""" else """""") +

-        """(?>""" +

-        // First alternative is if the start is an unescaped Block start. In that case you must

-        // have an unescaped block end followed directly by an unescaped delimiter as the termination.

-        (if (justification == TextStringJustification.Right

-          || justification == TextStringJustification.Center)

-          """(?:%6$s*)""" //+ // trim padChar off on the left. Aka right or center justified.

-        else """""") +

-        """(?<!(?<!%1$s)%2$s)%4$s""" + // unescaped block start

-        """(.*)""" + // captured content (before)

-        """(?<!(?<!%1$s)%2$s)%5$s""" + // unescaped block end

-        (if (justification == TextStringJustification.Left

-          || justification == TextStringJustification.Center)

-          """(?:%6$s*)""" //+ // trim padChar off on the right. aka left or center justified.

-        else """""") +

-        """(?<!(?<!%1$s)%2$s)(%3$s)""" + // unescaped delimiter (delim)

-        """(.*)""" + // trailing stuff (after)

-        """|""" +

-        // Second alternative is if the start is NOT a block start, in which case we are looking 

-        // for an unescaped delimiter at the end.

-        (if (justification == TextStringJustification.Right

-          || justification == TextStringJustification.Center)

-          """(?:%6$s*)""" //+ // trim padChar off. Aka right or center justified.

-        else """""") +

-        """(?<!%4$s)""" + // not a block start

-        """(.*?)""" + // lazy content so it won't absorb the padChars (before2)

-        (if (justification == TextStringJustification.Left

-          || justification == TextStringJustification.Center)

-          """(?:%6$s*)""" //+ // trim padChar off. aka left or center justified.

-        else """""") +

-        """(?<!(?<!%1$s)%2$s)(%3$s)""" + // unescaped delimiter (delim2)

-        """(.*)""" + // trailing stuff (after2)

-        """)"""

-    val contentPattern = str.format(escapeEscape, escape, delim, bStart, bEnd, padChar).r

-    contentPattern

-  }

+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification
+
+object DFDLRegularExpressions {
+
+  def getEscapeRegEx(escape: String, escapeEscape: String, delim: String,
+                     dotMatchesAll: Boolean = true) = {
+    val str =
+      (if (dotMatchesAll) """(?s)""" else """""") +
+        """(.*?)""" + // content [before]. lazy so it won't absorb pad characters
+        """(?:(?<!(?:(?<!%1$s)%2$s))(%3$s))""" + // unescaped delimiter [delim]
+        """(.*)""" // trailing stuff [after]
+    val contentPattern = str.format(escapeEscape, escape, delim).r
+    contentPattern
+  }
+
+  def getEscapeRegExWithPadding(escape: String, escapeEscape: String, delim: String,
+                                padChar: String, justification: TextStringJustification,
+                                dotMatchesAll: Boolean = true) = {
+    val str =
+      (if (dotMatchesAll) """(?s)""" else """""") +
+        (if (justification == TextStringJustification.Right
+          || justification == TextStringJustification.Center)
+          """(?:%4$s*)""" // trim pad char. aka right or center justified.
+        else """""") +
+        """(.*?)""" + // content [before]. lazy so it won't absorb pad characters
+        (if (justification == TextStringJustification.Left
+          || justification == TextStringJustification.Center)
+          """(?:%4$s*)""" // trim pad char. aka left or center justified.
+        else """""") +
+        """(?:(?<!(?:(?<!%1$s)%2$s))(%3$s))""" + // unescaped delimiter [delim]
+        """(.*)""" // trailing stuff [after]
+    val contentPattern = str.format(escapeEscape, escape, delim, padChar).r
+    contentPattern
+  }
+
+  def getSameEscapeRegEx(escape: String, delim: String,
+                         dotMatchesAll: Boolean = true) = {
+    val str =
+      (if (dotMatchesAll) """(?s)""" else """""") +
+        """(.*?)""" + // content [before]. lazy so it won't absorb pad characters
+        """(?:""" +
+        """(?<!%1$s)((?:%1$s%1$s)*)(%2$s)""" + // unescaped delimiter [delim] which is delim preceded by NOT an odd number of escapes.
+        """)""" +
+        """(.*)""" // trailing stuff [after]
+    val contentPattern = str.format(escape, delim).r
+    contentPattern
+  }
+
+  def getSameEscapeRegExWithPadding(escape: String, delim: String,
+                                    padChar: String, justification: TextStringJustification,
+                                    dotMatchesAll: Boolean = true) = {
+    val str =
+      (if (dotMatchesAll) """(?s)""" else """""") +
+        (if (justification == TextStringJustification.Right
+          || justification == TextStringJustification.Center)
+          """(?<!%1$s)((?:%1$s%1$s)*)(?:%3$s*)""" // trim unescaped pad char. aka right or center justified.
+        else """""") +
+        """(.*?)""" + // content [before]. lazy so it won't absorb pad characters
+        """(?:""" +
+        (if (justification == TextStringJustification.Left
+          || justification == TextStringJustification.Center)
+          """(?<!%1$s)((?:%1$s%1$s)*)(?:%3$s*)(?<!%1$s)(%2$s)""" + // trim unescaped pad char. aka left or center justified. Then delim. (and if pad is size zero, then delimiter must be unescaped.)
+          """|""" // OR
+        else """""") +
+        """(?<!%1$s)((?:%1$s%1$s)*)(%2$s)""" + // unescaped delimiter [delim] which is delim preceded by NOT an odd number of escapes.
+        """)""" +
+        """(.*)""" // trailing stuff [after]
+    val contentPattern = str.format(escape, delim, padChar).r
+    //System.err.println(contentPattern)
+    contentPattern
+  }
+
+  def getEscapeBlockRegEx(bStart: String, bEnd: String, escapeEscape: String,
+                          escape: String, delim: String, dotMatchesAll: Boolean = true) = {
+    // Based upon assumption that String.format ignores extra arguments for which there
+    // are no specifiers
+    //
+    val str =
+      (if (dotMatchesAll) """(?s)""" else """""") +
+        """(?>""" +
+        // First alternative is if the start is an unescaped Block start. In that case you must
+        // have an unescaped block end followed directly by an unescaped delimiter as the termination.
+        """(?<!(?<!%1$s)%2$s)%4$s""" + // unescaped block start
+        """(.*)""" + // captured content (before)
+        """(?<!(?<!%1$s)%2$s)%5$s""" + // unescaped block end
+        """(?<!(?<!%1$s)%2$s)(%3$s)""" + // unescaped delimiter (delim)
+        """(.*)""" + // trailing stuff (after)
+        """|""" +
+        // Second alternative is if the start is NOT a block start, in which case we are looking 
+        // for an unescaped delimiter at the end.
+        """(?<!%4$s)""" + // not a block start
+        """(.*?)""" + // lazy content so it won't absorb the padChars (before2)
+        """(?<!(?<!%1$s)%2$s)(%3$s)""" + // unescaped delimiter (delim2)
+        """(.*)""" + // trailing stuff (after2)
+        """)"""
+    val contentPattern = str.format(escapeEscape, escape, delim, bStart, bEnd).r
+    contentPattern
+  }
+
+  def getEscapeBlockRegExWithPadding(bStart: String, bEnd: String, escapeEscape: String,
+                                     escape: String, padChar: String, delim: String, justification: TextStringJustification,
+                                     dotMatchesAll: Boolean = true) = {
+    // Based upon assumption that String.format ignores extra arguments for which there
+    // are no specifiers
+    //
+    val str =
+      (if (dotMatchesAll) """(?s)""" else """""") +
+        """(?>""" +
+        // First alternative is if the start is an unescaped Block start. In that case you must
+        // have an unescaped block end followed directly by an unescaped delimiter as the termination.
+        (if (justification == TextStringJustification.Right
+          || justification == TextStringJustification.Center)
+          """(?:%6$s*)""" //+ // trim padChar off on the left. Aka right or center justified.
+        else """""") +
+        """(?<!(?<!%1$s)%2$s)%4$s""" + // unescaped block start
+        """(.*)""" + // captured content (before)
+        """(?<!(?<!%1$s)%2$s)%5$s""" + // unescaped block end
+        (if (justification == TextStringJustification.Left
+          || justification == TextStringJustification.Center)
+          """(?:%6$s*)""" //+ // trim padChar off on the right. aka left or center justified.
+        else """""") +
+        """(?<!(?<!%1$s)%2$s)(%3$s)""" + // unescaped delimiter (delim)
+        """(.*)""" + // trailing stuff (after)
+        """|""" +
+        // Second alternative is if the start is NOT a block start, in which case we are looking 
+        // for an unescaped delimiter at the end.
+        (if (justification == TextStringJustification.Right
+          || justification == TextStringJustification.Center)
+          """(?:%6$s*)""" //+ // trim padChar off. Aka right or center justified.
+        else """""") +
+        """(?<!%4$s)""" + // not a block start
+        """(.*?)""" + // lazy content so it won't absorb the padChars (before2)
+        (if (justification == TextStringJustification.Left
+          || justification == TextStringJustification.Center)
+          """(?:%6$s*)""" //+ // trim padChar off. aka left or center justified.
+        else """""") +
+        """(?<!(?<!%1$s)%2$s)(%3$s)""" + // unescaped delimiter (delim2)
+        """(.*)""" + // trailing stuff (after2)
+        """)"""
+    val contentPattern = str.format(escapeEscape, escape, delim, bStart, bEnd, padChar).r
+    contentPattern
+  }
 }
\ No newline at end of file
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/PrimitivesDelimiters.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/PrimitivesDelimiters.scala
index 0765d2b..fc65799 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/PrimitivesDelimiters.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/PrimitivesDelimiters.scala
@@ -1,922 +1,922 @@
-package edu.illinois.ncsa.daffodil.processors

-

-/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.

- *

- * Developed by: Tresys Technology, LLC

- *               http://www.tresys.com

- * 

- * Permission is hereby granted, free of charge, to any person obtaining a copy of

- * this software and associated documentation files (the "Software"), to deal with

- * the Software without restriction, including without limitation the rights to

- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies

- * of the Software, and to permit persons to whom the Software is furnished to do

- * so, subject to the following conditions:

- * 

- *  1. Redistributions of source code must retain the above copyright notice,

- *     this list of conditions and the following disclaimers.

- * 

- *  2. Redistributions in binary form must reproduce the above copyright

- *     notice, this list of conditions and the following disclaimers in the

- *     documentation and/or other materials provided with the distribution.

- * 

- *  3. Neither the names of Tresys Technology, nor the names of its contributors

- *     may be used to endorse or promote products derived from this Software

- *     without specific prior written permission.

- * 

- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

- * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE

- * SOFTWARE.

- */

-

-import java.math.BigInteger

-import java.text.{ ParseException, ParsePosition }

-import java.util.regex.Pattern

-import java.nio.{ CharBuffer, ByteBuffer }

-import java.nio.charset.Charset

-import java.nio.charset.MalformedInputException

-import scala.collection.mutable.Queue

-import edu.illinois.ncsa.daffodil.Implicits._

-import edu.illinois.ncsa.daffodil.dsom._

-import edu.illinois.ncsa.daffodil.compiler._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.{ YesNo, LengthKind, ByteOrder, LengthUnits }

-import edu.illinois.ncsa.daffodil.util.{ Debug, LogLevel, Logging, Info }

-import edu.illinois.ncsa.daffodil.util.Misc.bytes2Hex

-import edu.illinois.ncsa.daffodil.processors._

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import edu.illinois.ncsa.daffodil.exceptions.UnsuppressableException

-import com.ibm.icu.text.{ NumberFormat, DecimalFormat }

-import edu.illinois.ncsa.daffodil.grammar.Terminal

-import scala.util.parsing.input.{ Reader }

-import java.sql.Timestamp

-import edu.illinois.ncsa.daffodil.grammar.Gram

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextTrimKind

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextNumberJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextCalendarJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextBooleanJustification

-import edu.illinois.ncsa.daffodil.processors.{ Parser => DaffodilParser }

-import scala.util.parsing.combinator.RegexParsers

-

-abstract class StaticDelimiter(kindString: String, delim: String, e: Term, guard: Boolean = true)

-  extends StaticText(delim, e, kindString, guard)

-

-abstract class StaticText(delim: String, e: Term, kindString: String, guard: Boolean = true)

-  extends Text(e, guard) //extends DelimParserBase(e, guard)

-  with WithParseErrorThrowing with TextReader {

-

-  val charset = e.knownEncodingCharset

-  val term = e.asInstanceOf[Term]

-

-  val staticTexts = delim.split("\\s").toList

-  val staticTextsCooked: Queue[String] = new Queue

-

-  staticTexts.foreach(x => staticTextsCooked.enqueue(EntityReplacer.replaceAll(x, Some(e))))

-

-  val delimsRaw = e.allTerminatingMarkup.map {

-    case (delimValue, elemName, elemPath) => (delimValue.constantAsString, elemName, elemPath)

-  }

-  val delimsCooked1 = delimsRaw.map {

-    case (delimValue, elemName, elemPath) => {

-      (new ListOfStringValueAsLiteral(delimValue.toString, e).cooked, elemName, elemPath)

-    }

-  }

-  val delimsCooked = delimsCooked1.map { case (delimValue, _, _) => delimValue }.flatten

-

-  // Here we expect that remoteDelims shall be defined as those delimiters who are not

-  // also defined locally.  That is to say that local should win over remote.

-  val remoteDelims = delimsCooked.toSet.diff(staticTextsCooked.toSet)

-

-  // here we define the parsers so that they are pre-compiled/generated

-  val delimParser = new DFDLDelimParserStatic(e.knownEncodingStringBitLengthFunction)

-  val (pInputDelimiterParser, pIsLocalDelimParser, remoteDelimRegex) =

-    delimParser.generateInputDelimiterParsers(staticTextsCooked.toSet, remoteDelims)

-

-  def parseMethod(input: Reader[Char]): DelimParseResult = {

-    val result: DelimParseResult = delimParser.parseInputDelimiter(pInputDelimiterParser, pIsLocalDelimParser, input)

-    result

-  }

-

-  //e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' not supported.")

-  Assert.invariant(delim != "") // shouldn't be here at all in this case.

-

-  def parser: DaffodilParser = new PrimParser(this, e) {

-

-    override def toBriefXML(depthLimit: Int = -1) = {

-      "<" + kindString + ">" + delim + " " + delimsRaw + "</" + kindString + ">"

-    }

-

-    override def toString = kindString + "('" + delim + "')" //  with terminating markup: " + term.prettyTerminatingMarkup + ")"

-

-    // This has to stay here, moving it outside of PrimParser causes it

-    // to not be defined. Why?

-    e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' not supported.")

-

-    val eName = e.toString()

-

-    def parse(start: PState): PState = withParseErrorThrowing(start) {

-      // withLoggingLevel(LogLevel.Debug) 

-      {

-

-        log(LogLevel.Debug, "%s - Parsing delimiter at byte position: %s", eName, (start.bitPos >> 3))

-        log(LogLevel.Debug, "%s - Parsing delimiter at bit position: %s", eName, start.bitPos)

-

-        log(LogLevel.Debug, "%s - Looking for local(%s) not remote (%s).", eName, staticTextsCooked.toSet, remoteDelims)

-

-        val bytePos = (start.bitPos >> 3).toInt

-

-        log(LogLevel.Debug, "Retrieving reader state.")

-        val reader = getReader(charset, start.bitPos, start)

-

-        // Well they may not be delimiters, but the logic is the same as for a 

-        // set of static delimiters.

-        val result = parseMethod(reader)

-

-        log(LogLevel.Debug, "%s - %s - DelimParseResultult: %s", this.toString(), eName, result)

-

-        result match {

-          case _: DelimParseFailure => {

-            log(LogLevel.Debug, "%s - %s: Delimiter not found!", this.toString(), eName)

-            return PE(start, "%s - %s: Delimiter not found!", this.toString(), eName)

-          }

-          case s: DelimParseSuccess if (s.delimiterLoc == DelimiterLocation.Remote) => {

-            val (remoteDelimValue, remoteElemName, remoteElemPath) =

-              getMatchedDelimiterInfo(remoteDelimRegex, s.delimiter, delimsCooked1)

-

-            log(LogLevel.Debug, "%s - %s: Found delimiter (%s) for %s when looking for %s(%s) for %s %s",

-              this.toString(), eName, remoteDelimValue, remoteElemPath, kindString, staticTexts.mkString(" "), e.path, positionalInfo)

-            return PE(start, "%s - %s: Found delimiter (%s) for %s when looking for %s(%s) for %s %s",

-              this.toString(), eName, remoteDelimValue, remoteElemPath, kindString, staticTexts.mkString(" "), e.path, positionalInfo)

-          }

-          case s: DelimParseSuccess =>

-            {

-              val numBits = e.knownEncodingStringBitLengthFunction(s.delimiter)

-              val endCharPos = if (start.charPos == -1) s.delimiter.length else start.charPos + s.delimiter.length()

-              val endBitPosDelim = numBits + start.bitPos

-

-              log(LogLevel.Debug, "%s - Found %s", eName, s.delimiter)

-              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPosDelim >> 3))

-              log(LogLevel.Debug, "%s - Ended at bit position %s", eName, endBitPosDelim)

-

-              return start.withPos(endBitPosDelim, endCharPos, Some(s.next))

-            }

-            start

-        }

-      }

-    }

-  }

-

-  def unparser: Unparser = new Unparser(e) {

-    val t = e.asInstanceOf[Term]

-    override def toString = "StaticText('" + delim + "' with terminating markup: " + t.prettyTerminatingMarkup + ")"

-    // setLoggingLevel(LogLevel.Info)

-    e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' is not supported.")

-    Assert.invariant(delim != "") //shouldn't be here at all in this case

-

-    def unparse(start: UState): UState = {

-      val encoder = e.knownEncodingCharset.newEncoder()

-      start.outStream.setEncoder(encoder)

-      start.outStream.fillCharBuffer(unparserDelim)

-      log(LogLevel.Debug, "Unparsed: " + start.outStream.getData)

-      start

-    }

-  }

-

-  def unparserDelim: String

-}

-

-abstract class Text(e: Term, guard: Boolean) extends DelimParserBase(e, guard) {

-  lazy val oes = {

-    val oes = e.optionEscapeScheme

-    oes.foreach { es =>

-      e.schemaDefinitionUnless(es.isKnownEscapeCharacter != Some(false), "Runtime expressions for escapeCharacters are not supported.")

-      e.schemaDefinitionUnless(es.isKnownEscapeEscapeCharacter != Some(false), "Runtime expressions for escapeEscapeCharacters are not supported.")

-    }

-    oes

-  }

-  lazy val esObj = EscapeScheme.getEscapeScheme(oes, e)

-  val eName = e.toString()

-

-  val positionalInfo = {

-    if (e.isDirectChildOfSequence) {

-      e.nearestEnclosingSequence match {

-        case Some(es) => {

-          val pos = e.positionInNearestEnclosingSequence - 1

-          if (es.hasPrefixSep) {

-            if (e.hasPriorRequiredSiblings) {

-              val prior: Term = es.groupMembers(pos - 1)

-              "after " + prior.prettyName + " and before " + eName

-            } else "before " + eName

-          } else if (es.hasInfixSep)

-            if (e.hasPriorRequiredSiblings && e.hasLaterRequiredSiblings) {

-              val prior: Term = es.groupMembers(pos - 1)

-

-              "after " + prior.prettyName + " and before " + eName

-            } else if (e.hasPriorRequiredSiblings) {

-              val prior: Term = es.groupMembers(pos - 1)

-              "after " + prior.prettyName + " and before " + eName

-            } else if (e.hasLaterRequiredSiblings) {

-              val later: Term = es.groupMembers(pos + 1)

-              "before " + later.prettyName

-            } else { "" }

-          else if (es.hasPostfixSep)

-            if (e.hasPriorRequiredSiblings && e.hasLaterRequiredSiblings) {

-              val later: Term = es.groupMembers(pos + 1)

-

-              "after " + eName + " and before " + later.prettyName

-            } else if (e.hasPriorRequiredSiblings) {

-              val prior: Term = es.groupMembers(pos - 1)

-              "after " + prior.prettyName + " and before " + eName

-            } else if (e.hasLaterRequiredSiblings) {

-              val later: Term = es.groupMembers(pos + 1)

-              "before " + later.prettyName

-            } else { "" }

-          else

-            ""

-        }

-        case None => ""

-      }

-    }

-  }

-

-  def getMatchedDelimiterInfo(remoteDelimRegex: Set[(String, String)], foundDelimiter: String,

-    delimiters: List[(List[String], String, String)]) = {

-    val matchedDelim = remoteDelimRegex.find {

-      case (delimRegex, _) => {

-        foundDelimiter.matches("(?s)^(" + delimRegex + ")$")

-      }

-    } match {

-      case Some((_, theValue)) => theValue

-      case None => Assert.impossibleCase()

-    }

-

-    val (remoteDelimValue, remoteElemName, remoteElemPath, _) =

-      {

-        val findResult = delimiters.map {

-          case (delimValueList, elemName, elemPath) => {

-            delimValueList.find(delim => delim == matchedDelim) match {

-              case Some(d) => (d, elemName, elemPath, true)

-              case None => (delimValueList.mkString(","), elemName, elemPath, false)

-            }

-          }

-        }.toSet.filter { x => x._4 == true }

-

-        if (findResult.size == 0) Assert.impossibleCase()

-        findResult.head

-      }

-    (remoteDelimValue, remoteElemName, remoteElemPath)

-  }

-

-}

-

-abstract class DynamicText(delimExpr: CompiledExpression, e: Term, kindString: String, guard: Boolean = true)

-  extends Text(e, guard)

-  with WithParseErrorThrowing with TextReader {

-

-  val charset = e.knownEncodingCharset

-  val term = e.asInstanceOf[Term]

-

-  // If there are any static delimiters, pre-process them here

-  lazy val staticDelimsRaw =

-    e.allTerminatingMarkup.filter {

-      case (delimValue, _, _) => delimValue.isConstant

-    }.map {

-      case (delimValue, eName, ePath) => (delimValue.constantAsString, eName, ePath)

-    }

-  lazy val staticDelimsCooked1 = staticDelimsRaw.map {

-    case (delimValue, elemName, elemPath) => { (new ListOfStringValueAsLiteral(delimValue.toString, e).cooked, elemName, elemPath) }

-  }

-  lazy val staticDelimsCooked = staticDelimsCooked1.map { case (delimValue, _, _) => delimValue }.flatten

-  lazy val (staticDelimsParsers, staticDelimsRegex) = dp.generateDelimiter(staticDelimsCooked.toSet)

-

-  def parseMethod(pInputDelimiterParser: dp.Parser[String],

-    pIsLocalDelimParser: dp.Parser[String],

-    input: Reader[Char]): DelimParseResult = {

-    val result: DelimParseResult = dp.parseInputDelimiter(pInputDelimiterParser, pIsLocalDelimParser, input)

-    result

-  }

-

-  def parser: DaffodilParser = new PrimParser(this, e) {

-

-    override def toBriefXML(depthLimit: Int = -1) = {

-      "<" + kindString + ">" + delimExpr + " " + delimExpr + "</" + kindString + ">"

-    }

-

-    e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' not supported.")

-

-    Assert.invariant(delimExpr.toString != "") // shouldn't be here at all in this case.

-    override def toString = kindString + "('" + delimExpr + "')" //  with terminating markup: " + term.prettyTerminatingMarkup + ")"

-

-    lazy val tm = e.allTerminatingMarkup

-    val eName = e.toString()

-

-    def parse(start: PState): PState = withParseErrorThrowing(start) {

-      // withLoggingLevel(LogLevel.Debug) 

-      {

-

-        // We must feed variable context out of one evaluation and into the next.

-        // So that the resulting variable map has the updated status of all evaluated variables.

-        var vars = start.variableMap

-

-        val dynamicDelimsRaw = e.allTerminatingMarkup.filter { case (delimValue, elemName, elemPath) => !delimValue.isConstant }.map {

-          case (delimValue, elemName, elemPath) =>

-            {

-              val R(res, newVMap) = delimValue.evaluate(start.parentElement, vars, start)

-              vars = newVMap

-              (res, elemName, elemPath)

-            }

-        }

-        // Dynamic delimiters can only be evaluated at runtime

-        val dynamicDelimsCooked1 = dynamicDelimsRaw.map {

-          case (delimValue, elemValue, elemPath) => { (new ListOfStringValueAsLiteral(delimValue.toString, e).cooked, elemValue, elemPath) }

-        }

-        val dynamicDelimsCooked = dynamicDelimsCooked1.map { case (delimValue, _, _) => delimValue }.flatten

-        val delimsCooked = dynamicDelimsCooked.union(staticDelimsCooked)

-        val (dynamicDelimsParsers, dynamicDelimsRegex) = dp.generateDelimiter(dynamicDelimsCooked.toSet)

-

-        val localDelimsRaw = {

-          val R(res, newVMap) = delimExpr.evaluate(start.parentElement, vars, start)

-          vars = newVMap

-          res

-        }

-        val localDelimsCooked1 = new ListOfStringValueAsLiteral(localDelimsRaw.toString(), e).cooked

-        val localDelimsCooked = localDelimsCooked1

-        val (localDelimsParser, localDelimsRegex) = dp.generateDelimiter(localDelimsCooked.toSet)

-

-        val pIsLocalDelimParser = dp.generateIsLocalDelimParser(localDelimsRegex)

-

-        val postEvalState = start.withVariables(vars)

-

-        log(LogLevel.Debug, "%s - Parsing delimiter at byte position: %s", eName, (postEvalState.bitPos >> 3))

-        log(LogLevel.Debug, "%s - Parsing delimiter at bit position: %s", eName, postEvalState.bitPos)

-

-        log(LogLevel.Debug, "%s - Looking for local(%s) not remote (%s).", eName, localDelimsCooked.toSet, delimsCooked.toSet)

-

-        val bytePos = (postEvalState.bitPos >> 3).toInt

-

-        log(LogLevel.Debug, "Retrieving reader state.")

-        val reader = getReader(charset, start.bitPos, postEvalState)

-

-        val remoteDelims: Array[dp.Parser[String]] = staticDelimsParsers.union(dynamicDelimsParsers)

-        val remoteDelimRegex = dp.getDelimsRegex(staticDelimsCooked.union(dynamicDelimsCooked).toSet)

-        val pInputDelimiterParser = dp.generateInputDelimiterParser(localDelimsParser, remoteDelims)

-

-        val result = parseMethod(pInputDelimiterParser, pIsLocalDelimParser, reader)

-

-        log(LogLevel.Debug, "%s - %s - DelimParseResultult: %s", this.toString(), eName, result)

-

-        result match {

-          case _: DelimParseFailure => {

-            log(LogLevel.Debug, "%s - %s: Delimiter not found!", this.toString(), eName)

-            return PE(start, "%s - %s: Delimiter not found!", this.toString(), eName)

-          }

-          case s: DelimParseSuccess if (s.delimiterLoc == DelimiterLocation.Remote) => {

-            val (remoteDelimValue, remoteElemName, remoteElemPath) =

-              getMatchedDelimiterInfo(remoteDelimRegex, s.delimiter, dynamicDelimsCooked1)

-

-            log(LogLevel.Debug, "%s - %s: Found delimiter (%s) for %s when looking for %s(%s) for %s",

-              this.toString(), eName, remoteDelimValue, remoteElemPath, kindString, localDelimsCooked.mkString(" "), e.path)

-            return PE(start, "%s - %s: Found delimiter (%s) for %s when looking for %s(%s) for %s",

-              this.toString(), eName, remoteDelimValue, remoteElemPath, kindString, localDelimsCooked.mkString(" "), e.path)

-          }

-          case s: DelimParseSuccess =>

-            {

-              val numBits = e.knownEncodingStringBitLengthFunction(s.delimiter)

-              val endCharPos = if (postEvalState.charPos == -1) s.delimiter.length else postEvalState.charPos + s.delimiter.length()

-              val endBitPosDelim = numBits + postEvalState.bitPos

-

-              log(LogLevel.Debug, "%s - Found %s", eName, s.delimiter)

-              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPosDelim >> 3))

-              log(LogLevel.Debug, "%s - Ended at bit position %s", eName, endBitPosDelim)

-

-              return postEvalState.withPos(endBitPosDelim, endCharPos, Some(s.next))

-            }

-            postEvalState

-        }

-      }

-    }

-  }

-

-  /*

-  def unparser: Unparser = new Unparser(e) {

-    val t = e.asInstanceOf[Term]

-    override def toString = "StaticText('" + delimExpr + "' with terminating markup: " + t.prettyTerminatingMarkup + ")"

-    // setLoggingLevel(LogLevel.Info)

-    e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' is not supported.")

-    Assert.invariant(delimExpr != "") //shouldn't be here at all in this case

-

-    def unparse(start: UState): UState = {

-      // We really want to do something similar to the below to evaluate the expression

-      // for a delimiter.

-      //      val localDelimsRaw = {

-      //        val R(res, newVMap) = delimExpr.evaluate(start.parentElement, vars, start)

-      //        vars = newVMap

-      //        res

-      //      }

-      //      val localDelimsCooked1 = new ListOfStringValueAsLiteral(localDelimsRaw.toString(), e).cooked

-      //      val localDelimsCooked = localDelimsCooked1

-      val encoder = e.knownEncodingCharset.newEncoder()

-      start.outStream.setEncoder(encoder)

-

-      // TODO: This is not correct, we need to be able to evaluate delimExpr and select a

-      // delimiter to use here.

-      start.outStream.fillCharBuffer(delimExpr.toString()) //start.outStream.fillCharBuffer(unparserDelim)

-      log(LogLevel.Debug, "Unparsed: " + start.outStream.getData))

-      start

-    }

-  } */

-  def unparser: Unparser = DummyUnparser

-

-  //def unparserDelim: String

-}

-

-abstract class DynamicDelimiter(kindString: String, delimExpr: CompiledExpression, e: Term, guard: Boolean = true)

-  extends DynamicText(delimExpr, e, kindString, guard)

-

-//case class StaticInitiator(e: Term) extends StaticDelimiter(e.initiator.constantAsString, e)

-case class StaticInitiator(e: Term) extends StaticDelimiter("Init", e.initiator.constantAsString, e) {

-  Assert.invariant(e.hasInitiator)

-  lazy val unparserDelim = e.initiator.constantAsString.split("""\s""").head

-}

-//case class StaticTerminator(e : Term) extends StaticDelimiter(e.terminator.constantAsString, e)

-case class StaticTerminator(e: Term) extends StaticDelimiter("Term", e.terminator.constantAsString, e) {

-  Assert.invariant(e.hasTerminator)

-  lazy val unparserDelim = e.terminator.constantAsString.split("""\s""").head

-}

-case class DynamicInitiator(e: Term) extends DynamicDelimiter("Init", e.initiator, e)

-case class DynamicTerminator(e: Term) extends DynamicDelimiter("Term", e.terminator, e)

-

-// Note: for a static separator, we pass s, the sequence, because that is where

-// the charset encoding comes from. 

-case class StaticSeparator(s: Sequence, t: Term) extends StaticDelimiter("Sep", s.separator.constantAsString, s) {

-  Assert.invariant(s.hasSeparator)

-  lazy val unparserDelim = s.separator.constantAsString.split("""\s""").head

-}

-case class DynamicSeparator(s: Sequence, t: Term) extends DynamicDelimiter("Sep", s.separator, s)

-

-case class LiteralNilExplicitLengthInBytes(e: ElementBase)

-  extends LiteralNilInBytesBase(e, "LiteralNilExplicit") {

-

-  val expr = e.length

-  val exprText = expr.prettyExpr

-

-  final def computeLength(start: PState) = {

-    val R(nBytesAsAny, newVMap) = expr.evaluate(start.parentElement, start.variableMap, start)

-    val nBytes = nBytesAsAny.toString().toLong //nBytesAsAny.asInstanceOf[Long]

-    (nBytes, newVMap)

-  }

-

-}

-

-case class LiteralNilKnownLengthInBytes(e: ElementBase, lengthInBytes: Long)

-  extends LiteralNilInBytesBase(e, "LiteralNilKnown") {

-

-  final def computeLength(start: PState) = {

-    (lengthInBytes, start.variableMap)

-  }

-

-}

-

-abstract class LiteralNilInBytesBase(e: ElementBase, label: String)

-  extends StaticText(e.nilValue, e, label, e.isNillable)

-  with Padded {

-

-  protected def computeLength(start: PState): (Long, VariableMap)

-

-  // We are to assume that we can always read nBytes

-  // a failure to read nBytes is a failure period.

-

-  lazy val unparserDelim = Assert.notYetImplemented()

-

-  override def parser = new PrimParser(this, e) {

-

-    override def toBriefXML(depthLimit: Int = -1): String = {

-      "<" + name + " nilValue='" + e.nilValue + "'/>"

-    }

-

-    val isEmptyAllowed = e.nilValue.contains("%ES;")

-    val eName = e.toString()

-    val nilValuesCooked = new ListOfStringValueAsLiteral(e.nilValue, e).cooked

-    val charsetName = charset.name()

-

-    def parse(start: PState): PState = {

-      //      withLoggingLevel(LogLevel.Debug) 

-      {

-

-        // TODO: What if someone passes in nBytes = 0 for Explicit length, is this legal?

-

-        val (nBytes: Long, newVMap: VariableMap) = computeLength(start)

-        val postEvalState = start.withVariables(newVMap)

-        log(LogLevel.Debug, "Explicit length %s", nBytes)

-

-        //val postEvalState = start //start.withVariables(vars)

-

-        log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, nilValuesCooked, nilValuesCooked.length)

-        val in = postEvalState.inStream

-

-        val bytePos = (postEvalState.bitPos >> 3).toInt

-        log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)

-        log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)

-

-        // some encodings aren't whole bytes

-        // if (postEvalState.bitPos % 8 != 0) { return PE(postEvalState, "LiteralNilPattern - not byte aligned.") }

-

-        val decoder = charset.newDecoder()

-        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)

-        try {

-          val reader = in.getCharReader(charset, postEvalState.bitPos)

-          val bytes = in.getBytes(postEvalState.bitPos, nBytes.toInt)

-          val cb = decoder.decode(ByteBuffer.wrap(bytes))

-          val result = cb.toString

-          val trimmedResult = d.removePadding(result, justificationTrim, padChar)

-          val endBitPos = postEvalState.bitPos + (nBytes.toInt * 8)

-          val endCharPos = if (postEvalState.charPos == -1) result.length() else postEvalState.charPos + result.length()

-

-          // We have a field, is it empty?

-          val isFieldEmpty = trimmedResult.length == 0 //result.length() == 0

-

-          if (isFieldEmpty && isEmptyAllowed) {

-            // Valid!

-            postEvalState.parentElement.makeNil()

-            return postEvalState // Empty, no need to advance

-          } else if (isFieldEmpty && !isEmptyAllowed) {

-            // Fail!

-            return PE(postEvalState, "%s - Empty field found but not allowed!", eName)

-          } else if (d.isFieldDfdlLiteral(trimmedResult, nilValuesCooked.toSet)) {

-            // Contains a nilValue, Success!

-            postEvalState.parentElement.makeNil()

-

-            log(LogLevel.Debug, "%s - Found %s", eName, trimmedResult)

-            log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))

-            log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)

-

-            return postEvalState.withPos(endBitPos, endCharPos, Some(reader)) // Need to advance past found nilValue

-          } else {

-            // Fail!

-            return PE(postEvalState, "%s - Does not contain a nil literal!", eName)

-          }

-        } catch {

-          case e: IndexOutOfBoundsException => {

-            // In this case, we failed to get the bytes

-            if (isEmptyAllowed) {

-              // Valid!

-              postEvalState.parentElement.makeNil()

-              return postEvalState // Empty, no need to advance

-            } else {

-              return PE(postEvalState, "%s - Insufficient Bytes in field; required %s", name, nBytes)

-            }

-          }

-          case u: UnsuppressableException => throw u

-          case e: Exception => { return PE(postEvalState, "%s - Exception: \n%s", name, e.getMessage()) }

-        }

-      }

-    }

-

-  }

-

-  override def unparser: Unparser = new Unparser(e) {

-    def unparse(start: UState): UState = {

-      Assert.notYetImplemented()

-    }

-  }

-}

-

-case class LiteralNilExplicitLengthInChars(e: ElementBase)

-  extends StaticText(e.nilValue, e, "LiteralNilExplicit", e.isNillable)

-  with Padded {

-  // We are to assume that we can always read nChars

-  // a failure to read nChars is a failure period.

-

-  // TODO: LiteralNilExplicitLengthInChars really is a variation of LiteralNilPattern

-  lazy val unparserDelim = Assert.notYetImplemented()

-

-  override def parser = new PrimParser(this, e) {

-

-    override def toBriefXML(depthLimit: Int = -1): String = {

-      "<" + name + " nilValue='" + e.nilValue + "'/>"

-    }

-

-    val isEmptyAllowed = e.nilValue.contains("%ES;")

-    val eName = e.toString()

-    val nilValuesCooked = new ListOfStringValueAsLiteral(e.nilValue, e).cooked

-    val charsetName = charset.name()

-    val expr = e.length

-    val exprText = expr.prettyExpr

-

-    def parse(start: PState): PState = {

-      // withLoggingLevel(LogLevel.Info) 

-      {

-

-        //val postEvalState = start //start.withVariables(vars)

-

-        val R(nCharsAsAny, newVMap) = expr.evaluate(start.parentElement, start.variableMap, start)

-        val nChars = nCharsAsAny.asInstanceOf[String] //nBytesAsAny.asInstanceOf[Long]

-        val postEvalState = start.withVariables(newVMap)

-        log(LogLevel.Debug, "Explicit length %s", nChars)

-

-        val pattern = "(?s)^.{%s}".format(nChars)

-

-        log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, nilValuesCooked, nilValuesCooked.length)

-

-        val bytePos = (postEvalState.bitPos >> 3).toInt

-        log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)

-        log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)

-

-        // Don't check this here. This can vary by encoding.

-        //if (postEvalState.bitPos % 8 != 0) { return PE(start, "LiteralNilPattern - not byte aligned.") }

-

-        log(LogLevel.Debug, "Retrieving reader state.")

-        val reader = getReader(charset, start.bitPos, start)

-

-        if (nChars == 0 && isEmptyAllowed) {

-          log(LogLevel.Debug, "%s - explicit length of 0 and %ES; found as nilValue.", eName)

-          postEvalState.parentElement.makeNil()

-          return postEvalState // Empty, no need to advance

-        }

-

-        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)

-

-        val result = d.parseInputPatterned(pattern, reader)

-

-        result match {

-          case _: DelimParseFailure =>

-            return PE(postEvalState, "%s - %s - Parse failed.", this.toString(), eName)

-          case s: DelimParseSuccess => {

-            // We have a field, is it empty?

-            val field = d.removePadding(s.field, justificationTrim, padChar)

-            val isFieldEmpty = field.length() == 0

-

-            if (isFieldEmpty && isEmptyAllowed) {

-              // Valid!

-              start.parentElement.makeNil()

-              return postEvalState // Empty, no need to advance

-            } else if (isFieldEmpty && !isEmptyAllowed) {

-              // Fail!

-              return PE(postEvalState, "%s - Empty field found but not allowed!", eName)

-            } else if (d.isFieldDfdlLiteral(field, nilValuesCooked.toSet)) {

-              // Contains a nilValue, Success!

-              start.parentElement.makeNil()

-

-              val numBits = s.numBits //e.knownEncodingStringBitLength(result.field)

-              val endCharPos =

-                if (postEvalState.charPos == -1) s.field.length

-                else postEvalState.charPos + s.field.length

-              val endBitPos = numBits + start.bitPos

-

-              log(LogLevel.Debug, "%s - Found %s", eName, s.field)

-              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))

-              log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)

-

-              return postEvalState.withPos(endBitPos, endCharPos, Some(s.next)) // Need to advance past found nilValue

-            } else {

-              // Fail!

-              return PE(postEvalState, "%s - Does not contain a nil literal!", eName)

-            }

-          }

-        }

-      }

-    }

-  }

-

-  override def unparser: Unparser = new Unparser(e) {

-    def unparse(start: UState): UState = {

-      Assert.notYetImplemented()

-    }

-  }

-

-}

-

-case class LiteralNilExplicit(e: ElementBase, nUnits: Long)

-  extends StaticText(e.nilValue, e, "LiteralNilExplicit", e.isNillable)

-  with Padded {

-  lazy val unparserDelim = Assert.notYetImplemented()

-  //val stParser = super.parser

-

-  override def parser = new PrimParser(this, e) {

-

-    override def toBriefXML(depthLimit: Int = -1): String = {

-      "<" + name + " nilValue='" + e.nilValue + "'/>"

-    }

-

-    val pattern = e.lengthPattern

-

-    val isEmptyAllowed = e.nilValue.contains("%ES;")

-    val eName = e.toString()

-    val nilValuesCooked = new ListOfStringValueAsLiteral(e.nilValue, e).cooked

-    val charsetName = charset.name()

-

-    def parse(start: PState): PState = {

-      // withLoggingLevel(LogLevel.Info) 

-      {

-

-        val postEvalState = start //start.withVariables(vars)

-

-        log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, nilValuesCooked, nilValuesCooked.length)

-

-        val bytePos = (postEvalState.bitPos >> 3).toInt

-        log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)

-        log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)

-

-        if (postEvalState.bitPos % 8 != 0) { return PE(start, "LiteralNilPattern - not byte aligned.") }

-

-        log(LogLevel.Debug, "Retrieving reader state.")

-        val reader = getReader(charset, start.bitPos, start)

-

-        //        val byteReader = in.byteReader.atPos(bytePos)

-        //        val reader = byteReader.charReader(decoder.charset().name())

-

-        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)

-

-        val result = d.parseInputPatterned(pattern, reader)

-

-        result match {

-          case _: DelimParseFailure =>

-            return PE(postEvalState, "%s - %s - Parse failed.", this.toString(), eName)

-          case s: DelimParseSuccess => {

-            // We have a field, is it empty?

-            val field = d.removePadding(s.field, justificationTrim, padChar)

-            val isFieldEmpty = field.length() == 0

-

-            if (isFieldEmpty && isEmptyAllowed) {

-              // Valid!

-              start.parentElement.makeNil()

-              return postEvalState // Empty, no need to advance

-            } else if (isFieldEmpty && !isEmptyAllowed) {

-              // Fail!

-              return PE(postEvalState, "%s - Empty field found but not allowed!", eName)

-            } else if (d.isFieldDfdlLiteral(field, nilValuesCooked.toSet)) {

-              // Contains a nilValue, Success!

-              start.parentElement.makeNil()

-

-              val numBits = s.numBits //e.knownEncodingStringBitLength(result.field)

-              //val endCharPos = start.charPos + result.field.length()

-              val endCharPos =

-                if (postEvalState.charPos == -1) s.field.length

-                else postEvalState.charPos + s.field.length

-              val endBitPos = numBits + start.bitPos

-

-              log(LogLevel.Debug, "%s - Found %s", eName, s.field)

-              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))

-              log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)

-

-              //return postEvalState.withPos(endBitPos, endCharPos) // Need to advance past found nilValue

-              return postEvalState.withPos(endBitPos, endCharPos, Some(s.next)) // Need to advance past found nilValue

-            } else {

-              // Fail!

-              return PE(postEvalState, "%s - Does not contain a nil literal!", eName)

-            }

-          }

-        }

-      }

-    }

-  }

-

-  override def unparser: Unparser = new Unparser(e) {

-    def unparse(start: UState): UState = {

-      Assert.notYetImplemented()

-    }

-  }

-}

-

-case class LiteralNilPattern(e: ElementBase)

-  extends StaticText(e.nilValue, e, "LiteralNilPattern", e.isNillable)

-  with Padded {

-  lazy val unparserDelim = Assert.notYetImplemented()

-  //val stParser = super.parser

-

-  override def parser = new PrimParser(this, e) {

-

-    override def toBriefXML(depthLimit: Int = -1): String = {

-      "<" + name + " nilValue='" + e.nilValue + "'/>"

-    }

-

-    val pattern = e.lengthPattern

-

-    val isEmptyAllowed = e.nilValue.contains("%ES;")

-    val eName = e.toString()

-    val nilValuesCooked = new ListOfStringValueAsLiteral(e.nilValue, e).cooked

-    val charsetName = charset.name()

-

-    def parse(start: PState): PState = {

-      // withLoggingLevel(LogLevel.Info) 

-      {

-

-        val postEvalState = start //start.withVariables(vars)

-

-        log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, nilValuesCooked, nilValuesCooked.length)

-

-        val bytePos = (postEvalState.bitPos >> 3).toInt

-        log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)

-        log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)

-

-        if (postEvalState.bitPos % 8 != 0) { return PE(start, "LiteralNilPattern - not byte aligned.") }

-

-        log(LogLevel.Debug, "Retrieving reader state.")

-        val reader = getReader(charset, start.bitPos, start)

-

-        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)

-

-        val result = d.parseInputPatterned(pattern, reader)

-

-        result match {

-          case _: DelimParseFailure =>

-            return PE(postEvalState, "%s - %s - Parse failed.", this.toString(), eName)

-          case s: DelimParseSuccess => {

-            // We have a field, is it empty?

-            val field = d.removePadding(s.field, justificationTrim, padChar)

-            val isFieldEmpty = field.length() == 0

-

-            if (isFieldEmpty && isEmptyAllowed) {

-              // Valid!

-              start.parentElement.makeNil()

-              return postEvalState // Empty, no need to advance

-            } else if (isFieldEmpty && !isEmptyAllowed) {

-              // Fail!

-              return PE(postEvalState, "%s - Empty field found but not allowed!", eName)

-            } else if (d.isFieldDfdlLiteral(field, nilValuesCooked.toSet)) {

-              // Contains a nilValue, Success!

-              start.parentElement.makeNil()

-

-              val numBits = s.numBits //e.knownEncodingStringBitLength(result.field)

-

-              val endCharPos =

-                if (postEvalState.charPos == -1) s.field.length

-                else postEvalState.charPos + s.field.length

-              val endBitPos = numBits + start.bitPos

-

-              log(LogLevel.Debug, "%s - Found %s", eName, s.field)

-              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))

-              log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)

-

-              return postEvalState.withPos(endBitPos, endCharPos, Some(s.next)) // Need to advance past found nilValue

-            } else {

-              // Fail!

-              return PE(postEvalState, "%s - Does not contain a nil literal!", eName)

-            }

-          }

-        }

-      }

-    }

-  }

-

-  override def unparser: Unparser = new Unparser(e) {

-    def unparse(start: UState): UState = {

-      Assert.notYetImplemented()

-    }

-  }

-}

-

-abstract class LiteralNilDelimitedEndOfData(eb: ElementBase)

-  extends StringDelimited(eb) {

-  val nilValuesCooked = new ListOfStringValueAsLiteral(eb.nilValue, eb).cooked

-  val isEmptyAllowed = eb.nilValue.contains("%ES;") // TODO: move outside parser

-

-  override def processResult(result: DelimParseResult, state: PState): PState = {

-    result match {

-      case f: DelimParseFailure =>

-        return parser.PE(state, "%s - %s - Parse failed.", this.toString(), eName)

-      case s: DelimParseSuccess => {

-        // We have a field, is it empty?

-        //val field = dp.removePadding(s.field, justificationTrim, padChar)

-        val field = s.field

-        val isFieldEmpty = field.length() == 0 // Note: field has been stripped of padChars

-

-        if (isFieldEmpty && !isEmptyAllowed) {

-          // Fail!

-          return parser.PE(state, "%s - Empty field found but not allowed!", eName)

-        } else if ((isFieldEmpty && isEmptyAllowed) || // Empty, but must advance past padChars if there were any. 

-          dp.isFieldDfdlLiteral(field, nilValuesCooked.toSet)) { // Not empty, but matches.

-          // Contains a nilValue, Success!

-          state.parentElement.makeNil()

-

-          val numBits = s.numBits

-          //val endCharPos = start.charPos + result.field.length()

-          val endCharPos = if (state.charPos == -1) s.numCharsRead else state.charPos + s.numCharsRead

-          val endBitPos = numBits + state.bitPos

-

-          log(LogLevel.Debug, "%s - Found %s", eName, s.field)

-          log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))

-          log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)

-

-          //return postEvalState.withPos(endBitPos, endCharPos) // Need to advance past found nilValue

-          return state.withPos(endBitPos, endCharPos, Some(s.next)) // Need to advance past found nilValue

-        } else {

-          // Fail!

-          return parser.PE(state, "%s - Does not contain a nil literal!", eName)

-        }

-      }

-    }

-  }

-}

-

-case class LiteralNilDelimitedEndOfDataStatic(eb: ElementBase)

-  extends LiteralNilDelimitedEndOfData(eb) with StaticDelim

-case class LiteralNilDelimitedEndOfDataDynamic(eb: ElementBase)

-  extends LiteralNilDelimitedEndOfData(eb) with DynamicDelim

-

-case class LogicalNilValue(e: ElementBase) extends Primitive(e, e.isNillable)

-

+package edu.illinois.ncsa.daffodil.processors
+
+/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
+ *
+ * Developed by: Tresys Technology, LLC
+ *               http://www.tresys.com
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal with
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ * 
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimers.
+ * 
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimers in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *  3. Neither the names of Tresys Technology, nor the names of its contributors
+ *     may be used to endorse or promote products derived from this Software
+ *     without specific prior written permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
+ * SOFTWARE.
+ */
+
+import java.math.BigInteger
+import java.text.{ ParseException, ParsePosition }
+import java.util.regex.Pattern
+import java.nio.{ CharBuffer, ByteBuffer }
+import java.nio.charset.Charset
+import java.nio.charset.MalformedInputException
+import scala.collection.mutable.Queue
+import edu.illinois.ncsa.daffodil.Implicits._
+import edu.illinois.ncsa.daffodil.dsom._
+import edu.illinois.ncsa.daffodil.compiler._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.{ YesNo, LengthKind, ByteOrder, LengthUnits }
+import edu.illinois.ncsa.daffodil.util.{ Debug, LogLevel, Logging, Info }
+import edu.illinois.ncsa.daffodil.util.Misc.bytes2Hex
+import edu.illinois.ncsa.daffodil.processors._
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import edu.illinois.ncsa.daffodil.exceptions.UnsuppressableException
+import com.ibm.icu.text.{ NumberFormat, DecimalFormat }
+import edu.illinois.ncsa.daffodil.grammar.Terminal
+import scala.util.parsing.input.{ Reader }
+import java.sql.Timestamp
+import edu.illinois.ncsa.daffodil.grammar.Gram
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextTrimKind
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextNumberJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextCalendarJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextBooleanJustification
+import edu.illinois.ncsa.daffodil.processors.{ Parser => DaffodilParser }
+import scala.util.parsing.combinator.RegexParsers
+
+abstract class StaticDelimiter(kindString: String, delim: String, e: Term, guard: Boolean = true)
+  extends StaticText(delim, e, kindString, guard)
+
+abstract class StaticText(delim: String, e: Term, kindString: String, guard: Boolean = true)
+  extends Text(e, guard) //extends DelimParserBase(e, guard)
+  with WithParseErrorThrowing with TextReader {
+
+  val charset = e.knownEncodingCharset
+  val term = e.asInstanceOf[Term]
+
+  val staticTexts = delim.split("\\s").toList
+  val staticTextsCooked: Queue[String] = new Queue
+
+  staticTexts.foreach(x => staticTextsCooked.enqueue(EntityReplacer.replaceAll(x, Some(e))))
+
+  val delimsRaw = e.allTerminatingMarkup.map {
+    case (delimValue, elemName, elemPath) => (delimValue.constantAsString, elemName, elemPath)
+  }
+  val delimsCooked1 = delimsRaw.map {
+    case (delimValue, elemName, elemPath) => {
+      (new ListOfStringValueAsLiteral(delimValue.toString, e).cooked, elemName, elemPath)
+    }
+  }
+  val delimsCooked = delimsCooked1.map { case (delimValue, _, _) => delimValue }.flatten
+
+  // Here we expect that remoteDelims shall be defined as those delimiters who are not
+  // also defined locally.  That is to say that local should win over remote.
+  val remoteDelims = delimsCooked.toSet.diff(staticTextsCooked.toSet)
+
+  // here we define the parsers so that they are pre-compiled/generated
+  val delimParser = new DFDLDelimParserStatic(e.knownEncodingStringBitLengthFunction)
+  val (pInputDelimiterParser, pIsLocalDelimParser, remoteDelimRegex) =
+    delimParser.generateInputDelimiterParsers(staticTextsCooked.toSet, remoteDelims)
+
+  def parseMethod(input: Reader[Char]): DelimParseResult = {
+    val result: DelimParseResult = delimParser.parseInputDelimiter(pInputDelimiterParser, pIsLocalDelimParser, input)
+    result
+  }
+
+  //e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' not supported.")
+  Assert.invariant(delim != "") // shouldn't be here at all in this case.
+
+  def parser: DaffodilParser = new PrimParser(this, e) {
+
+    override def toBriefXML(depthLimit: Int = -1) = {
+      "<" + kindString + ">" + delim + " " + delimsRaw + "</" + kindString + ">"
+    }
+
+    override def toString = kindString + "('" + delim + "')" //  with terminating markup: " + term.prettyTerminatingMarkup + ")"
+
+    // This has to stay here, moving it outside of PrimParser causes it
+    // to not be defined. Why?
+    e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' not supported.")
+
+    val eName = e.toString()
+
+    def parse(start: PState): PState = withParseErrorThrowing(start) {
+      // withLoggingLevel(LogLevel.Debug) 
+      {
+
+        log(LogLevel.Debug, "%s - Parsing delimiter at byte position: %s", eName, (start.bitPos >> 3))
+        log(LogLevel.Debug, "%s - Parsing delimiter at bit position: %s", eName, start.bitPos)
+
+        log(LogLevel.Debug, "%s - Looking for local(%s) not remote (%s).", eName, staticTextsCooked.toSet, remoteDelims)
+
+        val bytePos = (start.bitPos >> 3).toInt
+
+        log(LogLevel.Debug, "Retrieving reader state.")
+        val reader = getReader(charset, start.bitPos, start)
+
+        // Well they may not be delimiters, but the logic is the same as for a 
+        // set of static delimiters.
+        val result = parseMethod(reader)
+
+        log(LogLevel.Debug, "%s - %s - DelimParseResultult: %s", this.toString(), eName, result)
+
+        result match {
+          case _: DelimParseFailure => {
+            log(LogLevel.Debug, "%s - %s: Delimiter not found!", this.toString(), eName)
+            return PE(start, "%s - %s: Delimiter not found!", this.toString(), eName)
+          }
+          case s: DelimParseSuccess if (s.delimiterLoc == DelimiterLocation.Remote) => {
+            val (remoteDelimValue, remoteElemName, remoteElemPath) =
+              getMatchedDelimiterInfo(remoteDelimRegex, s.delimiter, delimsCooked1)
+
+            log(LogLevel.Debug, "%s - %s: Found delimiter (%s) for %s when looking for %s(%s) for %s %s",
+              this.toString(), eName, remoteDelimValue, remoteElemPath, kindString, staticTexts.mkString(" "), e.path, positionalInfo)
+            return PE(start, "%s - %s: Found delimiter (%s) for %s when looking for %s(%s) for %s %s",
+              this.toString(), eName, remoteDelimValue, remoteElemPath, kindString, staticTexts.mkString(" "), e.path, positionalInfo)
+          }
+          case s: DelimParseSuccess =>
+            {
+              val numBits = e.knownEncodingStringBitLengthFunction(s.delimiter)
+              val endCharPos = if (start.charPos == -1) s.delimiter.length else start.charPos + s.delimiter.length()
+              val endBitPosDelim = numBits + start.bitPos
+
+              log(LogLevel.Debug, "%s - Found %s", eName, s.delimiter)
+              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPosDelim >> 3))
+              log(LogLevel.Debug, "%s - Ended at bit position %s", eName, endBitPosDelim)
+
+              return start.withPos(endBitPosDelim, endCharPos, Some(s.next))
+            }
+            start
+        }
+      }
+    }
+  }
+
+  def unparser: Unparser = new Unparser(e) {
+    val t = e.asInstanceOf[Term]
+    override def toString = "StaticText('" + delim + "' with terminating markup: " + t.prettyTerminatingMarkup + ")"
+    // setLoggingLevel(LogLevel.Info)
+    e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' is not supported.")
+    Assert.invariant(delim != "") //shouldn't be here at all in this case
+
+    def unparse(start: UState): UState = {
+      val encoder = e.knownEncodingCharset.newEncoder()
+      start.outStream.setEncoder(encoder)
+      start.outStream.fillCharBuffer(unparserDelim)
+      log(LogLevel.Debug, "Unparsed: " + start.outStream.getData)
+      start
+    }
+  }
+
+  def unparserDelim: String
+}
+
+abstract class Text(e: Term, guard: Boolean) extends DelimParserBase(e, guard) {
+  lazy val oes = {
+    val oes = e.optionEscapeScheme
+    oes.foreach { es =>
+      e.schemaDefinitionUnless(es.isKnownEscapeCharacter != Some(false), "Runtime expressions for escapeCharacters are not supported.")
+      e.schemaDefinitionUnless(es.isKnownEscapeEscapeCharacter != Some(false), "Runtime expressions for escapeEscapeCharacters are not supported.")
+    }
+    oes
+  }
+  lazy val esObj = EscapeScheme.getEscapeScheme(oes, e)
+  val eName = e.toString()
+
+  val positionalInfo = {
+    if (e.isDirectChildOfSequence) {
+      e.nearestEnclosingSequence match {
+        case Some(es) => {
+          val pos = e.positionInNearestEnclosingSequence - 1
+          if (es.hasPrefixSep) {
+            if (e.hasPriorRequiredSiblings) {
+              val prior: Term = es.groupMembers(pos - 1)
+              "after " + prior.prettyName + " and before " + eName
+            } else "before " + eName
+          } else if (es.hasInfixSep)
+            if (e.hasPriorRequiredSiblings && e.hasLaterRequiredSiblings) {
+              val prior: Term = es.groupMembers(pos - 1)
+
+              "after " + prior.prettyName + " and before " + eName
+            } else if (e.hasPriorRequiredSiblings) {
+              val prior: Term = es.groupMembers(pos - 1)
+              "after " + prior.prettyName + " and before " + eName
+            } else if (e.hasLaterRequiredSiblings) {
+              val later: Term = es.groupMembers(pos + 1)
+              "before " + later.prettyName
+            } else { "" }
+          else if (es.hasPostfixSep)
+            if (e.hasPriorRequiredSiblings && e.hasLaterRequiredSiblings) {
+              val later: Term = es.groupMembers(pos + 1)
+
+              "after " + eName + " and before " + later.prettyName
+            } else if (e.hasPriorRequiredSiblings) {
+              val prior: Term = es.groupMembers(pos - 1)
+              "after " + prior.prettyName + " and before " + eName
+            } else if (e.hasLaterRequiredSiblings) {
+              val later: Term = es.groupMembers(pos + 1)
+              "before " + later.prettyName
+            } else { "" }
+          else
+            ""
+        }
+        case None => ""
+      }
+    }
+  }
+
+  def getMatchedDelimiterInfo(remoteDelimRegex: Set[(String, String)], foundDelimiter: String,
+    delimiters: List[(List[String], String, String)]) = {
+    val matchedDelim = remoteDelimRegex.find {
+      case (delimRegex, _) => {
+        foundDelimiter.matches("(?s)^(" + delimRegex + ")$")
+      }
+    } match {
+      case Some((_, theValue)) => theValue
+      case None => Assert.impossibleCase()
+    }
+
+    val (remoteDelimValue, remoteElemName, remoteElemPath, _) =
+      {
+        val findResult = delimiters.map {
+          case (delimValueList, elemName, elemPath) => {
+            delimValueList.find(delim => delim == matchedDelim) match {
+              case Some(d) => (d, elemName, elemPath, true)
+              case None => (delimValueList.mkString(","), elemName, elemPath, false)
+            }
+          }
+        }.toSet.filter { x => x._4 == true }
+
+        if (findResult.size == 0) Assert.impossibleCase()
+        findResult.head
+      }
+    (remoteDelimValue, remoteElemName, remoteElemPath)
+  }
+
+}
+
+abstract class DynamicText(delimExpr: CompiledExpression, e: Term, kindString: String, guard: Boolean = true)
+  extends Text(e, guard)
+  with WithParseErrorThrowing with TextReader {
+
+  val charset = e.knownEncodingCharset
+  val term = e.asInstanceOf[Term]
+
+  // If there are any static delimiters, pre-process them here
+  lazy val staticDelimsRaw =
+    e.allTerminatingMarkup.filter {
+      case (delimValue, _, _) => delimValue.isConstant
+    }.map {
+      case (delimValue, eName, ePath) => (delimValue.constantAsString, eName, ePath)
+    }
+  lazy val staticDelimsCooked1 = staticDelimsRaw.map {
+    case (delimValue, elemName, elemPath) => { (new ListOfStringValueAsLiteral(delimValue.toString, e).cooked, elemName, elemPath) }
+  }
+  lazy val staticDelimsCooked = staticDelimsCooked1.map { case (delimValue, _, _) => delimValue }.flatten
+  lazy val (staticDelimsParsers, staticDelimsRegex) = dp.generateDelimiter(staticDelimsCooked.toSet)
+
+  def parseMethod(pInputDelimiterParser: dp.Parser[String],
+    pIsLocalDelimParser: dp.Parser[String],
+    input: Reader[Char]): DelimParseResult = {
+    val result: DelimParseResult = dp.parseInputDelimiter(pInputDelimiterParser, pIsLocalDelimParser, input)
+    result
+  }
+
+  def parser: DaffodilParser = new PrimParser(this, e) {
+
+    override def toBriefXML(depthLimit: Int = -1) = {
+      "<" + kindString + ">" + delimExpr + " " + delimExpr + "</" + kindString + ">"
+    }
+
+    e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' not supported.")
+
+    Assert.invariant(delimExpr.toString != "") // shouldn't be here at all in this case.
+    override def toString = kindString + "('" + delimExpr + "')" //  with terminating markup: " + term.prettyTerminatingMarkup + ")"
+
+    lazy val tm = e.allTerminatingMarkup
+    val eName = e.toString()
+
+    def parse(start: PState): PState = withParseErrorThrowing(start) {
+      // withLoggingLevel(LogLevel.Debug) 
+      {
+
+        // We must feed variable context out of one evaluation and into the next.
+        // So that the resulting variable map has the updated status of all evaluated variables.
+        var vars = start.variableMap
+
+        val dynamicDelimsRaw = e.allTerminatingMarkup.filter { case (delimValue, elemName, elemPath) => !delimValue.isConstant }.map {
+          case (delimValue, elemName, elemPath) =>
+            {
+              val R(res, newVMap) = delimValue.evaluate(start.parentElement, vars, start)
+              vars = newVMap
+              (res, elemName, elemPath)
+            }
+        }
+        // Dynamic delimiters can only be evaluated at runtime
+        val dynamicDelimsCooked1 = dynamicDelimsRaw.map {
+          case (delimValue, elemValue, elemPath) => { (new ListOfStringValueAsLiteral(delimValue.toString, e).cooked, elemValue, elemPath) }
+        }
+        val dynamicDelimsCooked = dynamicDelimsCooked1.map { case (delimValue, _, _) => delimValue }.flatten
+        val delimsCooked = dynamicDelimsCooked.union(staticDelimsCooked)
+        val (dynamicDelimsParsers, dynamicDelimsRegex) = dp.generateDelimiter(dynamicDelimsCooked.toSet)
+
+        val localDelimsRaw = {
+          val R(res, newVMap) = delimExpr.evaluate(start.parentElement, vars, start)
+          vars = newVMap
+          res
+        }
+        val localDelimsCooked1 = new ListOfStringValueAsLiteral(localDelimsRaw.toString(), e).cooked
+        val localDelimsCooked = localDelimsCooked1
+        val (localDelimsParser, localDelimsRegex) = dp.generateDelimiter(localDelimsCooked.toSet)
+
+        val pIsLocalDelimParser = dp.generateIsLocalDelimParser(localDelimsRegex)
+
+        val postEvalState = start.withVariables(vars)
+
+        log(LogLevel.Debug, "%s - Parsing delimiter at byte position: %s", eName, (postEvalState.bitPos >> 3))
+        log(LogLevel.Debug, "%s - Parsing delimiter at bit position: %s", eName, postEvalState.bitPos)
+
+        log(LogLevel.Debug, "%s - Looking for local(%s) not remote (%s).", eName, localDelimsCooked.toSet, delimsCooked.toSet)
+
+        val bytePos = (postEvalState.bitPos >> 3).toInt
+
+        log(LogLevel.Debug, "Retrieving reader state.")
+        val reader = getReader(charset, start.bitPos, postEvalState)
+
+        val remoteDelims: Array[dp.Parser[String]] = staticDelimsParsers.union(dynamicDelimsParsers)
+        val remoteDelimRegex = dp.getDelimsRegex(staticDelimsCooked.union(dynamicDelimsCooked).toSet)
+        val pInputDelimiterParser = dp.generateInputDelimiterParser(localDelimsParser, remoteDelims)
+
+        val result = parseMethod(pInputDelimiterParser, pIsLocalDelimParser, reader)
+
+        log(LogLevel.Debug, "%s - %s - DelimParseResultult: %s", this.toString(), eName, result)
+
+        result match {
+          case _: DelimParseFailure => {
+            log(LogLevel.Debug, "%s - %s: Delimiter not found!", this.toString(), eName)
+            return PE(start, "%s - %s: Delimiter not found!", this.toString(), eName)
+          }
+          case s: DelimParseSuccess if (s.delimiterLoc == DelimiterLocation.Remote) => {
+            val (remoteDelimValue, remoteElemName, remoteElemPath) =
+              getMatchedDelimiterInfo(remoteDelimRegex, s.delimiter, dynamicDelimsCooked1)
+
+            log(LogLevel.Debug, "%s - %s: Found delimiter (%s) for %s when looking for %s(%s) for %s",
+              this.toString(), eName, remoteDelimValue, remoteElemPath, kindString, localDelimsCooked.mkString(" "), e.path)
+            return PE(start, "%s - %s: Found delimiter (%s) for %s when looking for %s(%s) for %s",
+              this.toString(), eName, remoteDelimValue, remoteElemPath, kindString, localDelimsCooked.mkString(" "), e.path)
+          }
+          case s: DelimParseSuccess =>
+            {
+              val numBits = e.knownEncodingStringBitLengthFunction(s.delimiter)
+              val endCharPos = if (postEvalState.charPos == -1) s.delimiter.length else postEvalState.charPos + s.delimiter.length()
+              val endBitPosDelim = numBits + postEvalState.bitPos
+
+              log(LogLevel.Debug, "%s - Found %s", eName, s.delimiter)
+              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPosDelim >> 3))
+              log(LogLevel.Debug, "%s - Ended at bit position %s", eName, endBitPosDelim)
+
+              return postEvalState.withPos(endBitPosDelim, endCharPos, Some(s.next))
+            }
+            postEvalState
+        }
+      }
+    }
+  }
+
+  /*
+  def unparser: Unparser = new Unparser(e) {
+    val t = e.asInstanceOf[Term]
+    override def toString = "StaticText('" + delimExpr + "' with terminating markup: " + t.prettyTerminatingMarkup + ")"
+    // setLoggingLevel(LogLevel.Info)
+    e.schemaDefinitionWarningUnless(e.ignoreCase == YesNo.No, "Property ignoreCase='yes' is not supported.")
+    Assert.invariant(delimExpr != "") //shouldn't be here at all in this case
+
+    def unparse(start: UState): UState = {
+      // We really want to do something similar to the below to evaluate the expression
+      // for a delimiter.
+      //      val localDelimsRaw = {
+      //        val R(res, newVMap) = delimExpr.evaluate(start.parentElement, vars, start)
+      //        vars = newVMap
+      //        res
+      //      }
+      //      val localDelimsCooked1 = new ListOfStringValueAsLiteral(localDelimsRaw.toString(), e).cooked
+      //      val localDelimsCooked = localDelimsCooked1
+      val encoder = e.knownEncodingCharset.newEncoder()
+      start.outStream.setEncoder(encoder)
+
+      // TODO: This is not correct, we need to be able to evaluate delimExpr and select a
+      // delimiter to use here.
+      start.outStream.fillCharBuffer(delimExpr.toString()) //start.outStream.fillCharBuffer(unparserDelim)
+      log(LogLevel.Debug, "Unparsed: " + start.outStream.getData))
+      start
+    }
+  } */
+  def unparser: Unparser = DummyUnparser
+
+  //def unparserDelim: String
+}
+
+abstract class DynamicDelimiter(kindString: String, delimExpr: CompiledExpression, e: Term, guard: Boolean = true)
+  extends DynamicText(delimExpr, e, kindString, guard)
+
+//case class StaticInitiator(e: Term) extends StaticDelimiter(e.initiator.constantAsString, e)
+case class StaticInitiator(e: Term) extends StaticDelimiter("Init", e.initiator.constantAsString, e) {
+  Assert.invariant(e.hasInitiator)
+  lazy val unparserDelim = e.initiator.constantAsString.split("""\s""").head
+}
+//case class StaticTerminator(e : Term) extends StaticDelimiter(e.terminator.constantAsString, e)
+case class StaticTerminator(e: Term) extends StaticDelimiter("Term", e.terminator.constantAsString, e) {
+  Assert.invariant(e.hasTerminator)
+  lazy val unparserDelim = e.terminator.constantAsString.split("""\s""").head
+}
+case class DynamicInitiator(e: Term) extends DynamicDelimiter("Init", e.initiator, e)
+case class DynamicTerminator(e: Term) extends DynamicDelimiter("Term", e.terminator, e)
+
+// Note: for a static separator, we pass s, the sequence, because that is where
+// the charset encoding comes from. 
+case class StaticSeparator(s: Sequence, t: Term) extends StaticDelimiter("Sep", s.separator.constantAsString, s) {
+  Assert.invariant(s.hasSeparator)
+  lazy val unparserDelim = s.separator.constantAsString.split("""\s""").head
+}
+case class DynamicSeparator(s: Sequence, t: Term) extends DynamicDelimiter("Sep", s.separator, s)
+
+case class LiteralNilExplicitLengthInBytes(e: ElementBase)
+  extends LiteralNilInBytesBase(e, "LiteralNilExplicit") {
+
+  val expr = e.length
+  val exprText = expr.prettyExpr
+
+  final def computeLength(start: PState) = {
+    val R(nBytesAsAny, newVMap) = expr.evaluate(start.parentElement, start.variableMap, start)
+    val nBytes = nBytesAsAny.toString().toLong //nBytesAsAny.asInstanceOf[Long]
+    (nBytes, newVMap)
+  }
+
+}
+
+case class LiteralNilKnownLengthInBytes(e: ElementBase, lengthInBytes: Long)
+  extends LiteralNilInBytesBase(e, "LiteralNilKnown") {
+
+  final def computeLength(start: PState) = {
+    (lengthInBytes, start.variableMap)
+  }
+
+}
+
+abstract class LiteralNilInBytesBase(e: ElementBase, label: String)
+  extends StaticText(e.nilValue, e, label, e.isNillable)
+  with Padded {
+
+  protected def computeLength(start: PState): (Long, VariableMap)
+
+  // We are to assume that we can always read nBytes
+  // a failure to read nBytes is a failure period.
+
+  lazy val unparserDelim = Assert.notYetImplemented()
+
+  override def parser = new PrimParser(this, e) {
+
+    override def toBriefXML(depthLimit: Int = -1): String = {
+      "<" + name + " nilValue='" + e.nilValue + "'/>"
+    }
+
+    val isEmptyAllowed = e.nilValue.contains("%ES;")
+    val eName = e.toString()
+    val nilValuesCooked = new ListOfStringValueAsLiteral(e.nilValue, e).cooked
+    val charsetName = charset.name()
+
+    def parse(start: PState): PState = {
+      //      withLoggingLevel(LogLevel.Debug) 
+      {
+
+        // TODO: What if someone passes in nBytes = 0 for Explicit length, is this legal?
+
+        val (nBytes: Long, newVMap: VariableMap) = computeLength(start)
+        val postEvalState = start.withVariables(newVMap)
+        log(LogLevel.Debug, "Explicit length %s", nBytes)
+
+        //val postEvalState = start //start.withVariables(vars)
+
+        log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, nilValuesCooked, nilValuesCooked.length)
+        val in = postEvalState.inStream
+
+        val bytePos = (postEvalState.bitPos >> 3).toInt
+        log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)
+        log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)
+
+        // some encodings aren't whole bytes
+        // if (postEvalState.bitPos % 8 != 0) { return PE(postEvalState, "LiteralNilPattern - not byte aligned.") }
+
+        val decoder = charset.newDecoder()
+        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)
+        try {
+          val reader = in.getCharReader(charset, postEvalState.bitPos)
+          val bytes = in.getBytes(postEvalState.bitPos, nBytes.toInt)
+          val cb = decoder.decode(ByteBuffer.wrap(bytes))
+          val result = cb.toString
+          val trimmedResult = d.removePadding(result, justificationTrim, padChar)
+          val endBitPos = postEvalState.bitPos + (nBytes.toInt * 8)
+          val endCharPos = if (postEvalState.charPos == -1) result.length() else postEvalState.charPos + result.length()
+
+          // We have a field, is it empty?
+          val isFieldEmpty = trimmedResult.length == 0 //result.length() == 0
+
+          if (isFieldEmpty && isEmptyAllowed) {
+            // Valid!
+            postEvalState.parentElement.makeNil()
+            return postEvalState // Empty, no need to advance
+          } else if (isFieldEmpty && !isEmptyAllowed) {
+            // Fail!
+            return PE(postEvalState, "%s - Empty field found but not allowed!", eName)
+          } else if (d.isFieldDfdlLiteral(trimmedResult, nilValuesCooked.toSet)) {
+            // Contains a nilValue, Success!
+            postEvalState.parentElement.makeNil()
+
+            log(LogLevel.Debug, "%s - Found %s", eName, trimmedResult)
+            log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))
+            log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)
+
+            return postEvalState.withPos(endBitPos, endCharPos, Some(reader)) // Need to advance past found nilValue
+          } else {
+            // Fail!
+            return PE(postEvalState, "%s - Does not contain a nil literal!", eName)
+          }
+        } catch {
+          case e: IndexOutOfBoundsException => {
+            // In this case, we failed to get the bytes
+            if (isEmptyAllowed) {
+              // Valid!
+              postEvalState.parentElement.makeNil()
+              return postEvalState // Empty, no need to advance
+            } else {
+              return PE(postEvalState, "%s - Insufficient Bytes in field; required %s", name, nBytes)
+            }
+          }
+          case u: UnsuppressableException => throw u
+          case e: Exception => { return PE(postEvalState, "%s - Exception: \n%s", name, e.getMessage()) }
+        }
+      }
+    }
+
+  }
+
+  override def unparser: Unparser = new Unparser(e) {
+    def unparse(start: UState): UState = {
+      Assert.notYetImplemented()
+    }
+  }
+}
+
+case class LiteralNilExplicitLengthInChars(e: ElementBase)
+  extends StaticText(e.nilValue, e, "LiteralNilExplicit", e.isNillable)
+  with Padded {
+  // We are to assume that we can always read nChars
+  // a failure to read nChars is a failure period.
+
+  // TODO: LiteralNilExplicitLengthInChars really is a variation of LiteralNilPattern
+  lazy val unparserDelim = Assert.notYetImplemented()
+
+  override def parser = new PrimParser(this, e) {
+
+    override def toBriefXML(depthLimit: Int = -1): String = {
+      "<" + name + " nilValue='" + e.nilValue + "'/>"
+    }
+
+    val isEmptyAllowed = e.nilValue.contains("%ES;")
+    val eName = e.toString()
+    val nilValuesCooked = new ListOfStringValueAsLiteral(e.nilValue, e).cooked
+    val charsetName = charset.name()
+    val expr = e.length
+    val exprText = expr.prettyExpr
+
+    def parse(start: PState): PState = {
+      // withLoggingLevel(LogLevel.Info) 
+      {
+
+        //val postEvalState = start //start.withVariables(vars)
+
+        val R(nCharsAsAny, newVMap) = expr.evaluate(start.parentElement, start.variableMap, start)
+        val nChars = nCharsAsAny.asInstanceOf[String] //nBytesAsAny.asInstanceOf[Long]
+        val postEvalState = start.withVariables(newVMap)
+        log(LogLevel.Debug, "Explicit length %s", nChars)
+
+        val pattern = "(?s)^.{%s}".format(nChars)
+
+        log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, nilValuesCooked, nilValuesCooked.length)
+
+        val bytePos = (postEvalState.bitPos >> 3).toInt
+        log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)
+        log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)
+
+        // Don't check this here. This can vary by encoding.
+        //if (postEvalState.bitPos % 8 != 0) { return PE(start, "LiteralNilPattern - not byte aligned.") }
+
+        log(LogLevel.Debug, "Retrieving reader state.")
+        val reader = getReader(charset, start.bitPos, start)
+
+        if (nChars == 0 && isEmptyAllowed) {
+          log(LogLevel.Debug, "%s - explicit length of 0 and %ES; found as nilValue.", eName)
+          postEvalState.parentElement.makeNil()
+          return postEvalState // Empty, no need to advance
+        }
+
+        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)
+
+        val result = d.parseInputPatterned(pattern, reader)
+
+        result match {
+          case _: DelimParseFailure =>
+            return PE(postEvalState, "%s - %s - Parse failed.", this.toString(), eName)
+          case s: DelimParseSuccess => {
+            // We have a field, is it empty?
+            val field = d.removePadding(s.field, justificationTrim, padChar)
+            val isFieldEmpty = field.length() == 0
+
+            if (isFieldEmpty && isEmptyAllowed) {
+              // Valid!
+              start.parentElement.makeNil()
+              return postEvalState // Empty, no need to advance
+            } else if (isFieldEmpty && !isEmptyAllowed) {
+              // Fail!
+              return PE(postEvalState, "%s - Empty field found but not allowed!", eName)
+            } else if (d.isFieldDfdlLiteral(field, nilValuesCooked.toSet)) {
+              // Contains a nilValue, Success!
+              start.parentElement.makeNil()
+
+              val numBits = s.numBits //e.knownEncodingStringBitLength(result.field)
+              val endCharPos =
+                if (postEvalState.charPos == -1) s.field.length
+                else postEvalState.charPos + s.field.length
+              val endBitPos = numBits + start.bitPos
+
+              log(LogLevel.Debug, "%s - Found %s", eName, s.field)
+              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))
+              log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)
+
+              return postEvalState.withPos(endBitPos, endCharPos, Some(s.next)) // Need to advance past found nilValue
+            } else {
+              // Fail!
+              return PE(postEvalState, "%s - Does not contain a nil literal!", eName)
+            }
+          }
+        }
+      }
+    }
+  }
+
+  override def unparser: Unparser = new Unparser(e) {
+    def unparse(start: UState): UState = {
+      Assert.notYetImplemented()
+    }
+  }
+
+}
+
+case class LiteralNilExplicit(e: ElementBase, nUnits: Long)
+  extends StaticText(e.nilValue, e, "LiteralNilExplicit", e.isNillable)
+  with Padded {
+  lazy val unparserDelim = Assert.notYetImplemented()
+  //val stParser = super.parser
+
+  override def parser = new PrimParser(this, e) {
+
+    override def toBriefXML(depthLimit: Int = -1): String = {
+      "<" + name + " nilValue='" + e.nilValue + "'/>"
+    }
+
+    val pattern = e.lengthPattern
+
+    val isEmptyAllowed = e.nilValue.contains("%ES;")
+    val eName = e.toString()
+    val nilValuesCooked = new ListOfStringValueAsLiteral(e.nilValue, e).cooked
+    val charsetName = charset.name()
+
+    def parse(start: PState): PState = {
+      // withLoggingLevel(LogLevel.Info) 
+      {
+
+        val postEvalState = start //start.withVariables(vars)
+
+        log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, nilValuesCooked, nilValuesCooked.length)
+
+        val bytePos = (postEvalState.bitPos >> 3).toInt
+        log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)
+        log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)
+
+        if (postEvalState.bitPos % 8 != 0) { return PE(start, "LiteralNilPattern - not byte aligned.") }
+
+        log(LogLevel.Debug, "Retrieving reader state.")
+        val reader = getReader(charset, start.bitPos, start)
+
+        //        val byteReader = in.byteReader.atPos(bytePos)
+        //        val reader = byteReader.charReader(decoder.charset().name())
+
+        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)
+
+        val result = d.parseInputPatterned(pattern, reader)
+
+        result match {
+          case _: DelimParseFailure =>
+            return PE(postEvalState, "%s - %s - Parse failed.", this.toString(), eName)
+          case s: DelimParseSuccess => {
+            // We have a field, is it empty?
+            val field = d.removePadding(s.field, justificationTrim, padChar)
+            val isFieldEmpty = field.length() == 0
+
+            if (isFieldEmpty && isEmptyAllowed) {
+              // Valid!
+              start.parentElement.makeNil()
+              return postEvalState // Empty, no need to advance
+            } else if (isFieldEmpty && !isEmptyAllowed) {
+              // Fail!
+              return PE(postEvalState, "%s - Empty field found but not allowed!", eName)
+            } else if (d.isFieldDfdlLiteral(field, nilValuesCooked.toSet)) {
+              // Contains a nilValue, Success!
+              start.parentElement.makeNil()
+
+              val numBits = s.numBits //e.knownEncodingStringBitLength(result.field)
+              //val endCharPos = start.charPos + result.field.length()
+              val endCharPos =
+                if (postEvalState.charPos == -1) s.field.length
+                else postEvalState.charPos + s.field.length
+              val endBitPos = numBits + start.bitPos
+
+              log(LogLevel.Debug, "%s - Found %s", eName, s.field)
+              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))
+              log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)
+
+              //return postEvalState.withPos(endBitPos, endCharPos) // Need to advance past found nilValue
+              return postEvalState.withPos(endBitPos, endCharPos, Some(s.next)) // Need to advance past found nilValue
+            } else {
+              // Fail!
+              return PE(postEvalState, "%s - Does not contain a nil literal!", eName)
+            }
+          }
+        }
+      }
+    }
+  }
+
+  override def unparser: Unparser = new Unparser(e) {
+    def unparse(start: UState): UState = {
+      Assert.notYetImplemented()
+    }
+  }
+}
+
+case class LiteralNilPattern(e: ElementBase)
+  extends StaticText(e.nilValue, e, "LiteralNilPattern", e.isNillable)
+  with Padded {
+  lazy val unparserDelim = Assert.notYetImplemented()
+  //val stParser = super.parser
+
+  override def parser = new PrimParser(this, e) {
+
+    override def toBriefXML(depthLimit: Int = -1): String = {
+      "<" + name + " nilValue='" + e.nilValue + "'/>"
+    }
+
+    val pattern = e.lengthPattern
+
+    val isEmptyAllowed = e.nilValue.contains("%ES;")
+    val eName = e.toString()
+    val nilValuesCooked = new ListOfStringValueAsLiteral(e.nilValue, e).cooked
+    val charsetName = charset.name()
+
+    def parse(start: PState): PState = {
+      // withLoggingLevel(LogLevel.Info) 
+      {
+
+        val postEvalState = start //start.withVariables(vars)
+
+        log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, nilValuesCooked, nilValuesCooked.length)
+
+        val bytePos = (postEvalState.bitPos >> 3).toInt
+        log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)
+        log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)
+
+        if (postEvalState.bitPos % 8 != 0) { return PE(start, "LiteralNilPattern - not byte aligned.") }
+
+        log(LogLevel.Debug, "Retrieving reader state.")
+        val reader = getReader(charset, start.bitPos, start)
+
+        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)
+
+        val result = d.parseInputPatterned(pattern, reader)
+
+        result match {
+          case _: DelimParseFailure =>
+            return PE(postEvalState, "%s - %s - Parse failed.", this.toString(), eName)
+          case s: DelimParseSuccess => {
+            // We have a field, is it empty?
+            val field = d.removePadding(s.field, justificationTrim, padChar)
+            val isFieldEmpty = field.length() == 0
+
+            if (isFieldEmpty && isEmptyAllowed) {
+              // Valid!
+              start.parentElement.makeNil()
+              return postEvalState // Empty, no need to advance
+            } else if (isFieldEmpty && !isEmptyAllowed) {
+              // Fail!
+              return PE(postEvalState, "%s - Empty field found but not allowed!", eName)
+            } else if (d.isFieldDfdlLiteral(field, nilValuesCooked.toSet)) {
+              // Contains a nilValue, Success!
+              start.parentElement.makeNil()
+
+              val numBits = s.numBits //e.knownEncodingStringBitLength(result.field)
+
+              val endCharPos =
+                if (postEvalState.charPos == -1) s.field.length
+                else postEvalState.charPos + s.field.length
+              val endBitPos = numBits + start.bitPos
+
+              log(LogLevel.Debug, "%s - Found %s", eName, s.field)
+              log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))
+              log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)
+
+              return postEvalState.withPos(endBitPos, endCharPos, Some(s.next)) // Need to advance past found nilValue
+            } else {
+              // Fail!
+              return PE(postEvalState, "%s - Does not contain a nil literal!", eName)
+            }
+          }
+        }
+      }
+    }
+  }
+
+  override def unparser: Unparser = new Unparser(e) {
+    def unparse(start: UState): UState = {
+      Assert.notYetImplemented()
+    }
+  }
+}
+
+abstract class LiteralNilDelimitedEndOfData(eb: ElementBase)
+  extends StringDelimited(eb) {
+  val nilValuesCooked = new ListOfStringValueAsLiteral(eb.nilValue, eb).cooked
+  val isEmptyAllowed = eb.nilValue.contains("%ES;") // TODO: move outside parser
+
+  override def processResult(result: DelimParseResult, state: PState): PState = {
+    result match {
+      case f: DelimParseFailure =>
+        return parser.PE(state, "%s - %s - Parse failed.", this.toString(), eName)
+      case s: DelimParseSuccess => {
+        // We have a field, is it empty?
+        //val field = dp.removePadding(s.field, justificationTrim, padChar)
+        val field = s.field
+        val isFieldEmpty = field.length() == 0 // Note: field has been stripped of padChars
+
+        if (isFieldEmpty && !isEmptyAllowed) {
+          // Fail!
+          return parser.PE(state, "%s - Empty field found but not allowed!", eName)
+        } else if ((isFieldEmpty && isEmptyAllowed) || // Empty, but must advance past padChars if there were any. 
+          dp.isFieldDfdlLiteral(field, nilValuesCooked.toSet)) { // Not empty, but matches.
+          // Contains a nilValue, Success!
+          state.parentElement.makeNil()
+
+          val numBits = s.numBits
+          //val endCharPos = start.charPos + result.field.length()
+          val endCharPos = if (state.charPos == -1) s.numCharsRead else state.charPos + s.numCharsRead
+          val endBitPos = numBits + state.bitPos
+
+          log(LogLevel.Debug, "%s - Found %s", eName, s.field)
+          log(LogLevel.Debug, "%s - Ended at byte position %s", eName, (endBitPos >> 3))
+          log(LogLevel.Debug, "%s - Ended at bit position ", eName, endBitPos)
+
+          //return postEvalState.withPos(endBitPos, endCharPos) // Need to advance past found nilValue
+          return state.withPos(endBitPos, endCharPos, Some(s.next)) // Need to advance past found nilValue
+        } else {
+          // Fail!
+          return parser.PE(state, "%s - Does not contain a nil literal!", eName)
+        }
+      }
+    }
+  }
+}
+
+case class LiteralNilDelimitedEndOfDataStatic(eb: ElementBase)
+  extends LiteralNilDelimitedEndOfData(eb) with StaticDelim
+case class LiteralNilDelimitedEndOfDataDynamic(eb: ElementBase)
+  extends LiteralNilDelimitedEndOfData(eb) with DynamicDelim
+
+case class LogicalNilValue(e: ElementBase) extends Primitive(e, e.isNillable)
+
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/PrimitivesLengthKind.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/PrimitivesLengthKind.scala
index 9cb8505..0a86670 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/PrimitivesLengthKind.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/PrimitivesLengthKind.scala
@@ -1,672 +1,672 @@
-package edu.illinois.ncsa.daffodil.processors

-

-/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.

- *

- * Developed by: Tresys Technology, LLC

- *               http://www.tresys.com

- * 

- * Permission is hereby granted, free of charge, to any person obtaining a copy of

- * this software and associated documentation files (the "Software"), to deal with

- * the Software without restriction, including without limitation the rights to

- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies

- * of the Software, and to permit persons to whom the Software is furnished to do

- * so, subject to the following conditions:

- * 

- *  1. Redistributions of source code must retain the above copyright notice,

- *     this list of conditions and the following disclaimers.

- * 

- *  2. Redistributions in binary form must reproduce the above copyright

- *     notice, this list of conditions and the following disclaimers in the

- *     documentation and/or other materials provided with the distribution.

- * 

- *  3. Neither the names of Tresys Technology, nor the names of its contributors

- *     may be used to endorse or promote products derived from this Software

- *     without specific prior written permission.

- * 

- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

- * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE

- * SOFTWARE.

- */

-

-import java.math.BigInteger

-import java.text.{ ParseException, ParsePosition }

-import java.util.regex.Pattern

-import java.nio.{ CharBuffer, ByteBuffer }

-import java.nio.charset.Charset

-import java.nio.charset.MalformedInputException

-import scala.collection.mutable.Queue

-import edu.illinois.ncsa.daffodil.Implicits._

-import edu.illinois.ncsa.daffodil.dsom._

-import edu.illinois.ncsa.daffodil.compiler._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.{ YesNo, LengthKind, ByteOrder, LengthUnits }

-import edu.illinois.ncsa.daffodil.util.{ Debug, LogLevel, Logging, Info }

-import edu.illinois.ncsa.daffodil.util.Misc.bytes2Hex

-import edu.illinois.ncsa.daffodil.processors._

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import edu.illinois.ncsa.daffodil.exceptions.UnsuppressableException

-import com.ibm.icu.text.{ NumberFormat, DecimalFormat }

-import edu.illinois.ncsa.daffodil.grammar.Terminal

-import scala.util.parsing.input.{ Reader }

-import java.sql.Timestamp

-import edu.illinois.ncsa.daffodil.grammar.Gram

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextTrimKind

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextNumberJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextCalendarJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextBooleanJustification

-import edu.illinois.ncsa.daffodil.processors.{ Parser => DaffodilParser }

-

-abstract class StringLength(e: ElementBase)

-  extends DelimParserBase(e, true)

-  with TextReader

-  with Padded

-  with WithParseErrorThrowing {

-

-  val charset = e.knownEncodingCharset

-  val stringLengthInBitsFnc = e.knownEncodingStringBitLengthFunction

-  val codepointWidth = e.knownEncodingWidthInBits

-

-  //lazy val dp = new DFDLDelimParserStatic(e.knownEncodingStringBitLengthFunction)

-  lazy val removePaddingParser: Option[dp.Parser[String]] = dp.generateRemovePaddingParser(justificationTrim, padChar)

-

-  def lengthText: String

-  def parserName: String

-

-  def getLength(pstate: PState): (Long, PState)

-  def parseInput(start: PState, charset: Charset, nBytes: Long): PState

-

-  def parser: DaffodilParser = new PrimParser(this, e) {

-    override def toString = String.format("%sParser(%s)", parserName, lengthText)

-

-    def parse(pstate: PState): PState = withParseErrorThrowing(pstate) {

-

-      log(LogLevel.Debug, "Parsing starting at bit position: %s", pstate.bitPos)

-

-      val (nBytes, start) = getLength(pstate)

-      log(LogLevel.Debug, "Explicit length %s", nBytes)

-

-      if (start.bitPos % 8 != 0) { return PE(start, "%s - not byte aligned.", parserName) }

-

-      try {

-        val postState = parseInput(start, charset, nBytes)

-        return postState

-      } catch {

-        case m: MalformedInputException => { return PE(start, "%s - MalformedInputException: \n%s", parserName, m.getMessage()) }

-        case e: IndexOutOfBoundsException => { return PE(start, "%s - Insufficient Bits in field: IndexOutOfBounds: \n%s", parserName, e.getMessage()) }

-        case u: UnsuppressableException => throw u

-        case e: Exception => { return PE(start, "%s - Exception: \n%s", parserName, e.getStackTraceString) }

-      }

-      pstate

-    }

-  }

-

-  def unparser: Unparser = new Unparser(e) {

-    override def toString = String.format("%sUnparser(%s)", parserName, lengthText)

-    //    val encoder = e.knownEncodingEncoder

-

-    def unparse(start: UState): UState = {

-      Assert.notYetImplemented()

-    }

-  }

-}

-

-trait VariableLength { self: Terminal =>

-  // Length is an expression

-  private lazy val eb = self.context.asInstanceOf[ElementBase]

-  val expr = eb.length

-  val exprText = expr.prettyExpr

-

-  def getLength(pstate: PState): (Long, PState) = {

-    val R(lengthAsAny, newVMap) = expr.evaluate(pstate.parentElement, pstate.variableMap, pstate)

-    val length = lengthAsAny.asInstanceOf[Long]

-    val start = pstate.withVariables(newVMap)

-    (length, start)

-  }

-}

-

-trait FixedLength { self: Terminal =>

-  // Length is a constant

-  private lazy val eb = self.context.asInstanceOf[ElementBase]

-  //Assert.invariant(eb.knownEncodingWidthInBits != -1)

-  //  def getLength(pstate: PState): (Long, PState) = {

-  //    (eb.fixedLength, pstate)

-  //  }

-}

-

-abstract class StringLengthInChars(e: ElementBase, nChars: Long)

-  extends StringLength(e)

-  with WithParseErrorThrowing {

-

-  lazy val nCharParser = dp.generateInputNCharactersParser(nChars)

-

-  def getLength(pstate: PState): (Long, PState) = {

-    (nChars, pstate)

-  }

-

-  def parseInput(start: PState, charset: Charset, nChars: Long): PState = start

-

-  override def parser: DaffodilParser = new PrimParser(this, e) {

-    String.format("%sParser(%s)", parserName, lengthText)

-

-    def parse(start: PState): PState = withParseErrorThrowing(start) {

-

-      log(LogLevel.Debug, "Parsing starting at bit position: %s", start.bitPos)

-

-      // no longer require alignment (some encodings aren't whole bytes)

-      // if (start.bitPos % 8 != 0) { return PE(start, "StringFixedLengthInVariableWidthCharacters - not byte aligned.") }

-

-      log(LogLevel.Debug, "Retrieving reader")

-

-      val reader = getReader(charset, start.bitPos, start)

-

-      val result = dp.parseInputNCharacters(nCharParser, reader, removePaddingParser, justificationTrim)

-

-      result match {

-        case _: DelimParseFailure =>

-          return PE(start, "Parse failed to find exactly %s characters.", nChars)

-        case s: DelimParseSuccess => {

-

-          val parsedField = s.field

-          val parsedBits = s.numBits

-          val endBitPos = start.bitPos + parsedBits

-

-          log(LogLevel.Debug, "Parsed: %s", parsedField)

-          log(LogLevel.Debug, "Ended at bit position: %s", endBitPos)

-

-          val endCharPos = if (start.charPos == -1) nChars else start.charPos + nChars

-          val currentElement = start.parentElement

-          currentElement.setDataValue(parsedField)

-          val postState = start.withPos(endBitPos, endCharPos, Some(s.next))

-          postState

-        }

-      }

-    }

-  }

-

-}

-

-abstract class StringLengthInBytes(e: ElementBase)

-  extends StringLength(e) {

-

-  def formatValue(value: String): String = {

-    value

-  }

-

-  def parseInput(start: PState, charset: Charset, nBytes: Long): PState = {

-    val in = start.inStream

-    val decoder = charset.newDecoder()

-

-    val reader = getReader(charset, start.bitPos, start)

-

-    // This next block of lines needs to become functionality of the

-    // reader so it can be shared, and decoding is all called from one

-    // place. 

-    val bytes = in.getBytes(start.bitPos, nBytes.toInt)

-    val cb = decoder.decode(ByteBuffer.wrap(bytes))

-    val result = cb.toString

-    val endBitPos = start.bitPos + stringLengthInBitsFnc(result)

-    log(LogLevel.Debug, "Parsed: " + result)

-    log(LogLevel.Debug, "Ended at bit position " + endBitPos)

-    val endCharPos = start.charPos + result.length

-    // 

-    // Maintain our global count of number of characters.

-    // TODO: get rid of global counter for a dataProcessor-saved one. 

-    // 

-    DFDLCharCounter.incr(result.length)

-

-    val currentElement = start.parentElement

-    val trimmedResult = dp.removePadding(removePaddingParser, justificationTrim, result)

-    // Assert.invariant(currentElement.getName != "_document_")

-    // Note: this side effect is backtracked, because at points of uncertainty, pre-copies of a node are made

-    // and when backtracking occurs they are used to replace the nodes modified by sub-parsers.

-    currentElement.setDataValue(formatValue(trimmedResult))

-    // 

-    // if the number of bytes was a multiple of the codepointWidth then 

-    // we will have parsed all the bytes, so the endBitPos and endCharPos 

-    // are synchronized still. 

-    // 

-    val postState = {

-      // TODO: Shouldn't the 8 * nBytes really be codepointWidth * nBytes?

-      if ((endBitPos - start.bitPos) == (8 * nBytes)) {

-        start.withPos(endBitPos, endCharPos, Some(reader))

-      } else {

-        Assert.invariant((endBitPos - start.bitPos) < (8 * nBytes))

-        start.withPos(endBitPos, -1, None)

-        // -1 means a subsequent primitive will have to construct

-        // a new reader at said bitPosition              

-      }

-    }

-

-    return postState

-  }

-}

-

-abstract class HexBinaryLengthInBytes(e: ElementBase)

-  extends StringLengthInBytes(e) {

-

-  override val charset: Charset = Charset.forName("ISO-8859-1")

-  override val stringLengthInBitsFnc = {

-    def stringBitLength(str: String) = {

-      // variable width encoding, so we have to convert each character 

-      // We assume here that it will be a multiple of bytes

-      // that is, that variable-width encodings are all some number

-      // of bytes.

-      str.getBytes(charset).length * 8

-    }

-    stringBitLength _

-  }

-  override def formatValue(value: String) = {

-    val hexStr = value.map(c => c.toByte.formatted("%02X")).mkString

-    hexStr

-  }

-}

-

-case class HexBinaryFixedLengthInBytes(e: ElementBase, nBytes: Long)

-  extends HexBinaryLengthInBytes(e) with FixedLength {

-

-  lazy val parserName = "HexBinaryFixedLengthInBytes"

-  lazy val lengthText = e.length.constantAsString

-

-  def getLength(pstate: PState): (Long, PState) = {

-    (nBytes, pstate)

-  }

-}

-

-case class HexBinaryFixedLengthInBits(e: ElementBase, nBits: Long)

-  extends HexBinaryLengthInBytes(e) with FixedLength {

-

-  lazy val parserName = "HexBinaryFixedLengthInBits"

-  lazy val lengthText = e.length.constantAsString

-

-  def getLength(pstate: PState): (Long, PState) = {

-    val nBytes = scala.math.ceil(nBits / 8).toLong

-    (nBytes, pstate)

-  }

-}

-

-case class HexBinaryVariableLengthInBytes(e: ElementBase)

-  extends HexBinaryLengthInBytes(e) with VariableLength {

-

-  lazy val parserName = "HexBinaryVariableLengthInBytes"

-  lazy val lengthText = exprText

-}

-

-case class StringFixedLengthInBytesFixedWidthCharacters(e: ElementBase, nBytes: Long)

-  extends StringLengthInBytes(e)

-  with FixedLength {

-

-  lazy val parserName = "StringFixedLengthInBytesFixedWidthCharacters"

-  lazy val lengthText = e.length.constantAsString

-

-  def getLength(pstate: PState): (Long, PState) = {

-    (nBytes, pstate)

-  }

-  // val maxBytes = Compiler.maxFieldContentLengthInBytes

-  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.

-  //  var cbufSize = 0

-}

-

-case class StringFixedLengthInBytesVariableWidthCharacters(e: ElementBase, nBytes: Long)

-  extends StringLengthInBytes(e)

-  with FixedLength {

-

-  lazy val parserName = "StringFixedLengthInBytesVariableWidthCharacters"

-  lazy val lengthText = nBytes.toString()

-

-  def getLength(pstate: PState): (Long, PState) = {

-    (nBytes, pstate)

-  }

-

-  // val maxBytes = Compiler.maxFieldContentLengthInBytes

-  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.

-  //  var cbufSize = 0

-}

-

-case class StringFixedLengthInVariableWidthCharacters(e: ElementBase, numChars: Long)

-  extends StringLengthInChars(e, numChars)

-  with FixedLength {

-

-  lazy val parserName = "StringFixedLengthInVariableWidthCharacters"

-  lazy val lengthText = e.length.constantAsString

-

-  // val maxBytes = Compiler.maxFieldContentLengthInBytes

-  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.

-  //  var cbufSize = 0

-}

-

-case class StringVariableLengthInBytes(e: ElementBase)

-  //extends Terminal(e, true)

-  extends StringLengthInBytes(e)

-  with VariableLength {

-

-  lazy val parserName = "StringVariableLengthInBytes"

-  lazy val lengthText = exprText

-

-  // val maxBytes = Compiler.maxFieldContentLengthInBytes

-  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.

-  //  var cbufSize = 0

-}

-

-case class StringVariableLengthInBytesVariableWidthCharacters(e: ElementBase)

-  //extends Terminal(e, true)

-  extends StringLengthInBytes(e)

-  with VariableLength {

-

-  lazy val parserName = "StringVariableLengthInBytesVariableWidthCharacters"

-  lazy val lengthText = exprText

-

-  // val maxBytes = Compiler.maxFieldContentLengthInBytes

-  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.

-  //  var cbufSize = 0

-}

-

-case class StringVariableLengthInVariableWidthCharacters(e: ElementBase)

-  extends StringLengthInBytes(e)

-  with VariableLength {

-

-  lazy val parserName = "StringVariableLengthInVariableWidthCharacters"

-  lazy val lengthText = e.length.constantAsString

-

-  // val maxBytes = Compiler.maxFieldContentLengthInBytes

-  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.

-  //  var cbufSize = 0

-}

-

-case class StringPatternMatched(e: ElementBase)

-  extends Terminal(e, true)

-  with WithParseErrorThrowing with TextReader with Padded {

-

-  val charset = e.knownEncodingCharset

-

-  def parser: DaffodilParser = new PrimParser(this, e) {

-    override def toString = "StringPatternMatched"

-    val pattern = e.lengthPattern

-

-    // The pattern will always be defined

-    lazy val dp = new DFDLDelimParserStatic(e.knownEncodingStringBitLengthFunction)

-    lazy val patternParser = dp.generateInputPatternedParser(pattern)

-

-    // TODO: Add parameter for changing CharBuffer size

-

-    val eName = e.toString()

-

-    def parse(start: PState): PState = withParseErrorThrowing(start) {

-      // withLoggingLevel(LogLevel.Info) 

-      {

-

-        log(LogLevel.Debug, "StringPatternMatched - %s - Parsing pattern at byte position: %s", eName, (start.bitPos >> 3))

-        log(LogLevel.Debug, "StringPatternMatched - %s - Parsing pattern at bit position: %s", eName, start.bitPos)

-

-        // some encodings aren't whole bytes.

-        // if (start.bitPos % 8 != 0) { return PE(start, "StringPatternMatched - not byte aligned.") }

-

-        val bytePos = (start.bitPos >> 3).toInt

-

-        log(LogLevel.Debug, "Retrieving reader")

-

-        val reader = getReader(charset, start.bitPos, start)

-

-        //        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)

-        //        val result = d.parseInputPatterned(pattern, reader)

-        val result = dp.parseInputPatterned(patternParser, reader)

-

-        val postState = result match {

-          case _: DelimParseFailure => {

-            // TODO: Is this right? A no match constitutes zero length.  So Nil would need to be checked?

-            // Because we check for Nil first, this is valid and allowed.

-            PE(start, "%s: No match found!", this.toString())

-          }

-          case s: DelimParseSuccess => {

-            val endBitPos = start.bitPos + s.numBits

-            log(LogLevel.Debug, "StringPatternMatched - Parsed: %s", s.field)

-            log(LogLevel.Debug, "StringPatternMatched - Ended at bit position %s", endBitPos)

-

-            val endCharPos = if (start.charPos == -1) s.field.length() else start.charPos + s.field.length()

-            val currentElement = start.parentElement

-            currentElement.setDataValue(s.field)

-            start.withPos(endBitPos, endCharPos, Some(s.next))

-          }

-

-        }

-        postState

-      }

-    }

-  }

-

-  def unparser: Unparser = new Unparser(e) {

-    def unparse(start: UState): UState = {

-      Assert.notYetImplemented()

-    }

-  }

-}

-

-abstract class StringDelimited(e: ElementBase)

-  extends DelimParserBase(e, true)

-  with TextReader

-  with Padded

-  with WithParseErrorThrowing {

-  //

-  val es = e.optionEscapeScheme

-  val esObj = EscapeScheme.getEscapeScheme(es, e)

-  val tm = e.allTerminatingMarkup

-  val cname = toString

-

-  val eName = e.toString()

-  val charset = e.knownEncodingCharset

-  val elemBase = e

-

-  //val dp = new DFDLDelimParserStatic(e.knownEncodingStringBitLengthFunction)

-

-  // These static delims are used whether we're static or dynamic

-  // because even a dynamic can have some static from enclosing scopes.

-  //  val staticDelimsRaw = e.allTerminatingMarkup.filter(x => x.isConstant).map { _.constantAsString }

-  val staticDelimsRaw = e.allTerminatingMarkup.filter {

-    case (delimValue, _, _) => delimValue.isConstant

-  }.map {

-    case (delimValue, _, _) => delimValue.constantAsString

-  }

-  val staticDelimsCooked1 = staticDelimsRaw.map(raw => { new ListOfStringValueAsLiteral(raw.toString, e).cooked })

-  val staticDelimsCooked = staticDelimsCooked1.flatten

-  val (staticDelimsParser, staticDelimsRegex) = dp.generateDelimiter(staticDelimsCooked.toSet)

-  val combinedStaticDelimsParser = dp.combineLongest(staticDelimsParser)

-

-  //  def parseMethod(hasDelim: Boolean, delimsParser: dp.Parser[String], delimsRegex: Array[String],

-  //    reader: Reader[Char]): DelimParseResult

-

-  def parseMethod(hasDelim: Boolean, delimsParser: dp.Parser[String], delimsRegex: Array[String],

-    reader: Reader[Char]): DelimParseResult = {

-    // TODO: Change DFDLDelimParser calls to get rid of Array.empty[String] since we're only passing a single list the majority of the time.

-    if (esObj.escapeSchemeKind == EscapeSchemeKind.Block) {

-      val (escapeBlockParser, escapeBlockEndRegex, escapeEscapeRegex) = dp.generateEscapeBlockParsers2(delimsParser,

-        esObj.escapeBlockStart, esObj.escapeBlockEnd, esObj.escapeEscapeCharacter, justificationTrim, padChar, true)

-      val removeEscapeBlocksRegex = dp.removeEscapesBlocksRegex(escapeEscapeRegex, escapeBlockEndRegex)

-      val parseInputParser = dp.generateInputParser2(dp.emptyParser, delimsParser, Array.empty[String], delimsRegex,

-        hasDelim, justificationTrim, padChar, true)

-      dp.parseInputEscapeBlock(escapeBlockParser, dp.emptyParser, delimsParser, reader,

-        justificationTrim, removeEscapeBlocksRegex, parseInputParser)

-    } else if (esObj.escapeSchemeKind == EscapeSchemeKind.Character) {

-      val delimsRegexCombined = dp.combineDelimitersRegex(Array.empty[String], delimsRegex)

-      val escapeCharacterParser = dp.generateInputEscapeCharacterParser2(delimsParser, delimsRegexCombined,

-        hasDelim, esObj.escapeCharacter, esObj.escapeEscapeCharacter, justificationTrim, padChar, true)

-      val esRegex = dp.convertDFDLLiteralToRegex(esObj.escapeCharacter)

-      val esEsRegex = dp.convertDFDLLiteralToRegex(esObj.escapeEscapeCharacter)

-      val removeEscapeCharacterRegex = dp.generateRemoveEscapeCharactersSameRegex(esRegex)

-      val removeUnescapedEscapesRegex = dp.removeUnescapedEscapesRegex(esEsRegex, esRegex)

-      val removeEscapeEscapesThatEscapeRegex = dp.removeEscapeEscapesThatEscapeRegex(esEsRegex, esRegex)

-      val removeEscapeRegex = dp.removeEscapeRegex(esRegex)

-      dp.parseInputEscapeCharacter(escapeCharacterParser, dp.emptyParser, delimsParser, reader,

-        justificationTrim, removeEscapeCharacterRegex, removeUnescapedEscapesRegex,

-        removeEscapeEscapesThatEscapeRegex, removeEscapeRegex, esObj.escapeCharacter,

-        esObj.escapeEscapeCharacter)

-    } else {

-      //d.parseInput(Set.empty[String], delimsCooked.toSet, reader, justificationTrim, padChar)

-      val parseInputParser = dp.generateInputParser2(dp.emptyParser, delimsParser, Array.empty[String], delimsRegex,

-        hasDelim, justificationTrim, padChar, true)

-      dp.parseInput(parseInputParser, dp.emptyParser, delimsParser, reader, justificationTrim)

-    }

-

-  }

-

-  def getDelims(pstate: PState): (List[String], Array[String], dp.Parser[String], Option[VariableMap])

-

-  /**

-   * Called at compile time in static case, at runtime for dynamic case.

-   */

-  def errorIfDelimsHaveWSPStar(delims: List[String]): Unit = {

-    if (delims.filter(x => x == "%WSP*;").length > 0) {

-      // We cannot detect this error until expressions have been evaluated!

-      log(LogLevel.Debug, "%s - Failed due to WSP* detected as a delimiter for lengthKind=delimited.", eName)

-      elemBase.schemaDefinitionError("WSP* cannot be used as a delimiter when lengthKind=delimited.")

-    }

-  }

-

-  def processResult(result: DelimParseResult, state: PState): PState = {

-    result match {

-      case f: DelimParseFailure =>

-        return parser.PE(state, "%s - %s - Parse failed.", this.toString(), eName)

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val numBits = s.numBits

-        log(LogLevel.Debug, "%s - Parsed: %s Parsed Bytes: %s (bits %s)", eName, field, numBits / 8, numBits)

-        val endCharPos = if (state.charPos == -1) s.numCharsRead else state.charPos + s.numCharsRead

-        val endBitPos = state.bitPos + numBits

-        val currentElement = state.parentElement

-        currentElement.setDataValue(field)

-        return state.withPos(endBitPos, endCharPos, Some(s.next))

-      }

-    }

-  }

-

-  def parser: DaffodilParser = new PrimParser(this, e) {

-    //    override def toString = cname + "(" + tm.map { _.prettyExpr } + ")"

-    override def toString = cname + "(" + tm.map { case (delimValue, _, _) => delimValue.prettyExpr } + ")"

-

-    def parse(start: PState): PState = withParseErrorThrowing(start) {

-

-      val (delimsCooked, delimsRegex, delimsParser, vars) = getDelims(start)

-

-      // We must feed variable context out of one evaluation and into the next.

-      // So that the resulting variable map has the updated status of all evaluated variables.

-      val postEvalState = vars match {

-        case Some(v) => start.withVariables(v)

-        case None => start

-      }

-

-      log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, delimsCooked, delimsCooked.length)

-

-      val bytePos = (postEvalState.bitPos >> 3).toInt

-      log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)

-      log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)

-

-      val reader = getReader(charset, postEvalState.bitPos, postEvalState)

-      val hasDelim = delimsCooked.length > 0

-

-      val result = try {

-        parseMethod(hasDelim, delimsParser, delimsRegex, reader)

-      } catch {

-        case mie: MalformedInputException =>

-          throw new ParseError(e, Some(postEvalState), "Malformed input, length: %s", mie.getInputLength())

-      }

-      processResult(result, postEvalState)

-    }

-  }

-

-  def unparser: Unparser = new Unparser(e) {

-    //    override def toString = cname + "(" + tm.map { _.prettyExpr } + ")"

-    override def toString = cname + "(" + tm.map { case (delimValue, _, _) => delimValue.prettyExpr } + ")"

-

-    def unparse(start: UState): UState =

-      // withLoggingLevel(LogLevel.Info) 

-      {

-        val data = start.currentElement.getText

-

-        val encoder = charset.newEncoder()

-        start.outStream.setEncoder(encoder)

-        start.outStream.fillCharBuffer(data)

-        log(LogLevel.Debug, "Unparsed: " + start.outStream.getData)

-        start

-      }

-  }

-}

-

-trait StaticDelim { self: StringDelimited =>

-

-  // do this at creation time if we're static

-  errorIfDelimsHaveWSPStar(staticDelimsCooked)

-

-  def getDelims(pstate: PState): (List[String], Array[String], dp.Parser[String], Option[VariableMap]) = {

-    (staticDelimsCooked, staticDelimsRegex, combinedStaticDelimsParser, None)

-  }

-

-}

-

-case class StringDelimitedEndOfDataStatic(e: ElementBase)

-  extends StringDelimited(e) with StaticDelim

-

-trait DynamicDelim { self: StringDelimited =>

-

-  override def getDelims(pstate: PState): (List[String], Array[String], dp.Parser[String], Option[VariableMap]) = {

-    // We must feed variable context out of one evaluation and into the next.

-    // So that the resulting variable map has the updated status of all evaluated variables.

-    var vars = pstate.variableMap

-

-    val dynamicDelimsRaw = elemBase.allTerminatingMarkup.filter { case (delimValue, _, _) => !delimValue.isConstant }.map {

-      case (delimValue, _, _) =>

-        {

-          val R(res, newVMap) = delimValue.evaluate(pstate.parentElement, vars, pstate)

-          vars = newVMap

-          res

-        }

-    }

-    val dynamicDelimsCooked1 = dynamicDelimsRaw.map(raw => { new ListOfStringValueAsLiteral(raw.toString, elemBase).cooked })

-    val dynamicDelimsCooked = dynamicDelimsCooked1.flatten

-    val (dynamicDelimsParser, dynamicDelimsRegex) = dp.generateDelimiter(dynamicDelimsCooked.toSet)

-

-    // Combine dynamic and with static delims if they exist

-    val delimsParser = dp.combineLongest(dynamicDelimsParser.union(staticDelimsParser))

-    val delimsCooked = dynamicDelimsCooked.union(staticDelimsCooked)

-    val delimsRegex = dynamicDelimsRegex.union(staticDelimsRegex)

-    //

-    // moved check here to avoid need for another abstract method to 

-    // factor this out.

-    errorIfDelimsHaveWSPStar(delimsCooked)

-

-    (delimsCooked, delimsRegex, delimsParser, Some(vars))

-  }

-

-}

-

-case class StringDelimitedEndOfDataDynamic(e: ElementBase)

-  extends StringDelimited(e) with DynamicDelim

-

-abstract class HexBinaryDelimited(e: ElementBase) extends StringDelimited(e) {

-  override val charset: Charset = Charset.forName("ISO-8859-1")

-  override def processResult(result: DelimParseResult, state: PState): PState = {

-    result match {

-      case f: DelimParseFailure =>

-        return parser.PE(state, "%s - %s - Parse failed.", this.toString(), eName)

-      case s: DelimParseSuccess => {

-        val field = s.get

-        val numBits = s.numBits

-        log(LogLevel.Debug, "%s - Parsed: %s Parsed Bytes: %s (bits %s)", eName, field, numBits / 8, numBits)

-        val endCharPos = if (state.charPos == -1) s.numCharsRead else state.charPos + s.numCharsRead

-        val endBitPos = state.bitPos + numBits

-        val currentElement = state.parentElement

-        val hexStr = field.map(c => c.toByte.formatted("%02X")).mkString

-        currentElement.setDataValue(hexStr)

-        return state.withPos(endBitPos, endCharPos, Some(s.next))

-      }

-    }

-  }

-}

-

-case class HexBinaryDelimitedEndOfDataStatic(e: ElementBase)

-  extends HexBinaryDelimited(e) with StaticDelim

-

-case class HexBinaryDelimitedEndOfDataDynamic(e: ElementBase)

-  extends HexBinaryDelimited(e) with DynamicDelim

+package edu.illinois.ncsa.daffodil.processors
+
+/* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
+ *
+ * Developed by: Tresys Technology, LLC
+ *               http://www.tresys.com
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal with
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ * 
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimers.
+ * 
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimers in the
+ *     documentation and/or other materials provided with the distribution.
+ * 
+ *  3. Neither the names of Tresys Technology, nor the names of its contributors
+ *     may be used to endorse or promote products derived from this Software
+ *     without specific prior written permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
+ * SOFTWARE.
+ */
+
+import java.math.BigInteger
+import java.text.{ ParseException, ParsePosition }
+import java.util.regex.Pattern
+import java.nio.{ CharBuffer, ByteBuffer }
+import java.nio.charset.Charset
+import java.nio.charset.MalformedInputException
+import scala.collection.mutable.Queue
+import edu.illinois.ncsa.daffodil.Implicits._
+import edu.illinois.ncsa.daffodil.dsom._
+import edu.illinois.ncsa.daffodil.compiler._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.{ YesNo, LengthKind, ByteOrder, LengthUnits }
+import edu.illinois.ncsa.daffodil.util.{ Debug, LogLevel, Logging, Info }
+import edu.illinois.ncsa.daffodil.util.Misc.bytes2Hex
+import edu.illinois.ncsa.daffodil.processors._
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import edu.illinois.ncsa.daffodil.exceptions.UnsuppressableException
+import com.ibm.icu.text.{ NumberFormat, DecimalFormat }
+import edu.illinois.ncsa.daffodil.grammar.Terminal
+import scala.util.parsing.input.{ Reader }
+import java.sql.Timestamp
+import edu.illinois.ncsa.daffodil.grammar.Gram
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextTrimKind
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextNumberJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextCalendarJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextBooleanJustification
+import edu.illinois.ncsa.daffodil.processors.{ Parser => DaffodilParser }
+
+abstract class StringLength(e: ElementBase)
+  extends DelimParserBase(e, true)
+  with TextReader
+  with Padded
+  with WithParseErrorThrowing {
+
+  val charset = e.knownEncodingCharset
+  val stringLengthInBitsFnc = e.knownEncodingStringBitLengthFunction
+  val codepointWidth = e.knownEncodingWidthInBits
+
+  //lazy val dp = new DFDLDelimParserStatic(e.knownEncodingStringBitLengthFunction)
+  lazy val removePaddingParser: Option[dp.Parser[String]] = dp.generateRemovePaddingParser(justificationTrim, padChar)
+
+  def lengthText: String
+  def parserName: String
+
+  def getLength(pstate: PState): (Long, PState)
+  def parseInput(start: PState, charset: Charset, nBytes: Long): PState
+
+  def parser: DaffodilParser = new PrimParser(this, e) {
+    override def toString = String.format("%sParser(%s)", parserName, lengthText)
+
+    def parse(pstate: PState): PState = withParseErrorThrowing(pstate) {
+
+      log(LogLevel.Debug, "Parsing starting at bit position: %s", pstate.bitPos)
+
+      val (nBytes, start) = getLength(pstate)
+      log(LogLevel.Debug, "Explicit length %s", nBytes)
+
+      if (start.bitPos % 8 != 0) { return PE(start, "%s - not byte aligned.", parserName) }
+
+      try {
+        val postState = parseInput(start, charset, nBytes)
+        return postState
+      } catch {
+        case m: MalformedInputException => { return PE(start, "%s - MalformedInputException: \n%s", parserName, m.getMessage()) }
+        case e: IndexOutOfBoundsException => { return PE(start, "%s - Insufficient Bits in field: IndexOutOfBounds: \n%s", parserName, e.getMessage()) }
+        case u: UnsuppressableException => throw u
+        case e: Exception => { return PE(start, "%s - Exception: \n%s", parserName, e.getStackTraceString) }
+      }
+      pstate
+    }
+  }
+
+  def unparser: Unparser = new Unparser(e) {
+    override def toString = String.format("%sUnparser(%s)", parserName, lengthText)
+    //    val encoder = e.knownEncodingEncoder
+
+    def unparse(start: UState): UState = {
+      Assert.notYetImplemented()
+    }
+  }
+}
+
+trait VariableLength { self: Terminal =>
+  // Length is an expression
+  private lazy val eb = self.context.asInstanceOf[ElementBase]
+  val expr = eb.length
+  val exprText = expr.prettyExpr
+
+  def getLength(pstate: PState): (Long, PState) = {
+    val R(lengthAsAny, newVMap) = expr.evaluate(pstate.parentElement, pstate.variableMap, pstate)
+    val length = lengthAsAny.asInstanceOf[Long]
+    val start = pstate.withVariables(newVMap)
+    (length, start)
+  }
+}
+
+trait FixedLength { self: Terminal =>
+  // Length is a constant
+  private lazy val eb = self.context.asInstanceOf[ElementBase]
+  //Assert.invariant(eb.knownEncodingWidthInBits != -1)
+  //  def getLength(pstate: PState): (Long, PState) = {
+  //    (eb.fixedLength, pstate)
+  //  }
+}
+
+abstract class StringLengthInChars(e: ElementBase, nChars: Long)
+  extends StringLength(e)
+  with WithParseErrorThrowing {
+
+  lazy val nCharParser = dp.generateInputNCharactersParser(nChars)
+
+  def getLength(pstate: PState): (Long, PState) = {
+    (nChars, pstate)
+  }
+
+  def parseInput(start: PState, charset: Charset, nChars: Long): PState = start
+
+  override def parser: DaffodilParser = new PrimParser(this, e) {
+    String.format("%sParser(%s)", parserName, lengthText)
+
+    def parse(start: PState): PState = withParseErrorThrowing(start) {
+
+      log(LogLevel.Debug, "Parsing starting at bit position: %s", start.bitPos)
+
+      // no longer require alignment (some encodings aren't whole bytes)
+      // if (start.bitPos % 8 != 0) { return PE(start, "StringFixedLengthInVariableWidthCharacters - not byte aligned.") }
+
+      log(LogLevel.Debug, "Retrieving reader")
+
+      val reader = getReader(charset, start.bitPos, start)
+
+      val result = dp.parseInputNCharacters(nCharParser, reader, removePaddingParser, justificationTrim)
+
+      result match {
+        case _: DelimParseFailure =>
+          return PE(start, "Parse failed to find exactly %s characters.", nChars)
+        case s: DelimParseSuccess => {
+
+          val parsedField = s.field
+          val parsedBits = s.numBits
+          val endBitPos = start.bitPos + parsedBits
+
+          log(LogLevel.Debug, "Parsed: %s", parsedField)
+          log(LogLevel.Debug, "Ended at bit position: %s", endBitPos)
+
+          val endCharPos = if (start.charPos == -1) nChars else start.charPos + nChars
+          val currentElement = start.parentElement
+          currentElement.setDataValue(parsedField)
+          val postState = start.withPos(endBitPos, endCharPos, Some(s.next))
+          postState
+        }
+      }
+    }
+  }
+
+}
+
+abstract class StringLengthInBytes(e: ElementBase)
+  extends StringLength(e) {
+
+  def formatValue(value: String): String = {
+    value
+  }
+
+  def parseInput(start: PState, charset: Charset, nBytes: Long): PState = {
+    val in = start.inStream
+    val decoder = charset.newDecoder()
+
+    val reader = getReader(charset, start.bitPos, start)
+
+    // This next block of lines needs to become functionality of the
+    // reader so it can be shared, and decoding is all called from one
+    // place. 
+    val bytes = in.getBytes(start.bitPos, nBytes.toInt)
+    val cb = decoder.decode(ByteBuffer.wrap(bytes))
+    val result = cb.toString
+    val endBitPos = start.bitPos + stringLengthInBitsFnc(result)
+    log(LogLevel.Debug, "Parsed: " + result)
+    log(LogLevel.Debug, "Ended at bit position " + endBitPos)
+    val endCharPos = start.charPos + result.length
+    // 
+    // Maintain our global count of number of characters.
+    // TODO: get rid of global counter for a dataProcessor-saved one. 
+    // 
+    DFDLCharCounter.incr(result.length)
+
+    val currentElement = start.parentElement
+    val trimmedResult = dp.removePadding(removePaddingParser, justificationTrim, result)
+    // Assert.invariant(currentElement.getName != "_document_")
+    // Note: this side effect is backtracked, because at points of uncertainty, pre-copies of a node are made
+    // and when backtracking occurs they are used to replace the nodes modified by sub-parsers.
+    currentElement.setDataValue(formatValue(trimmedResult))
+    // 
+    // if the number of bytes was a multiple of the codepointWidth then 
+    // we will have parsed all the bytes, so the endBitPos and endCharPos 
+    // are synchronized still. 
+    // 
+    val postState = {
+      // TODO: Shouldn't the 8 * nBytes really be codepointWidth * nBytes?
+      if ((endBitPos - start.bitPos) == (8 * nBytes)) {
+        start.withPos(endBitPos, endCharPos, Some(reader))
+      } else {
+        Assert.invariant((endBitPos - start.bitPos) < (8 * nBytes))
+        start.withPos(endBitPos, -1, None)
+        // -1 means a subsequent primitive will have to construct
+        // a new reader at said bitPosition              
+      }
+    }
+
+    return postState
+  }
+}
+
+abstract class HexBinaryLengthInBytes(e: ElementBase)
+  extends StringLengthInBytes(e) {
+
+  override val charset: Charset = Charset.forName("ISO-8859-1")
+  override val stringLengthInBitsFnc = {
+    def stringBitLength(str: String) = {
+      // variable width encoding, so we have to convert each character 
+      // We assume here that it will be a multiple of bytes
+      // that is, that variable-width encodings are all some number
+      // of bytes.
+      str.getBytes(charset).length * 8
+    }
+    stringBitLength _
+  }
+  override def formatValue(value: String) = {
+    val hexStr = value.map(c => c.toByte.formatted("%02X")).mkString
+    hexStr
+  }
+}
+
+case class HexBinaryFixedLengthInBytes(e: ElementBase, nBytes: Long)
+  extends HexBinaryLengthInBytes(e) with FixedLength {
+
+  lazy val parserName = "HexBinaryFixedLengthInBytes"
+  lazy val lengthText = e.length.constantAsString
+
+  def getLength(pstate: PState): (Long, PState) = {
+    (nBytes, pstate)
+  }
+}
+
+case class HexBinaryFixedLengthInBits(e: ElementBase, nBits: Long)
+  extends HexBinaryLengthInBytes(e) with FixedLength {
+
+  lazy val parserName = "HexBinaryFixedLengthInBits"
+  lazy val lengthText = e.length.constantAsString
+
+  def getLength(pstate: PState): (Long, PState) = {
+    val nBytes = scala.math.ceil(nBits / 8).toLong
+    (nBytes, pstate)
+  }
+}
+
+case class HexBinaryVariableLengthInBytes(e: ElementBase)
+  extends HexBinaryLengthInBytes(e) with VariableLength {
+
+  lazy val parserName = "HexBinaryVariableLengthInBytes"
+  lazy val lengthText = exprText
+}
+
+case class StringFixedLengthInBytesFixedWidthCharacters(e: ElementBase, nBytes: Long)
+  extends StringLengthInBytes(e)
+  with FixedLength {
+
+  lazy val parserName = "StringFixedLengthInBytesFixedWidthCharacters"
+  lazy val lengthText = e.length.constantAsString
+
+  def getLength(pstate: PState): (Long, PState) = {
+    (nBytes, pstate)
+  }
+  // val maxBytes = Compiler.maxFieldContentLengthInBytes
+  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.
+  //  var cbufSize = 0
+}
+
+case class StringFixedLengthInBytesVariableWidthCharacters(e: ElementBase, nBytes: Long)
+  extends StringLengthInBytes(e)
+  with FixedLength {
+
+  lazy val parserName = "StringFixedLengthInBytesVariableWidthCharacters"
+  lazy val lengthText = nBytes.toString()
+
+  def getLength(pstate: PState): (Long, PState) = {
+    (nBytes, pstate)
+  }
+
+  // val maxBytes = Compiler.maxFieldContentLengthInBytes
+  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.
+  //  var cbufSize = 0
+}
+
+case class StringFixedLengthInVariableWidthCharacters(e: ElementBase, numChars: Long)
+  extends StringLengthInChars(e, numChars)
+  with FixedLength {
+
+  lazy val parserName = "StringFixedLengthInVariableWidthCharacters"
+  lazy val lengthText = e.length.constantAsString
+
+  // val maxBytes = Compiler.maxFieldContentLengthInBytes
+  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.
+  //  var cbufSize = 0
+}
+
+case class StringVariableLengthInBytes(e: ElementBase)
+  //extends Terminal(e, true)
+  extends StringLengthInBytes(e)
+  with VariableLength {
+
+  lazy val parserName = "StringVariableLengthInBytes"
+  lazy val lengthText = exprText
+
+  // val maxBytes = Compiler.maxFieldContentLengthInBytes
+  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.
+  //  var cbufSize = 0
+}
+
+case class StringVariableLengthInBytesVariableWidthCharacters(e: ElementBase)
+  //extends Terminal(e, true)
+  extends StringLengthInBytes(e)
+  with VariableLength {
+
+  lazy val parserName = "StringVariableLengthInBytesVariableWidthCharacters"
+  lazy val lengthText = exprText
+
+  // val maxBytes = Compiler.maxFieldContentLengthInBytes
+  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.
+  //  var cbufSize = 0
+}
+
+case class StringVariableLengthInVariableWidthCharacters(e: ElementBase)
+  extends StringLengthInBytes(e)
+  with VariableLength {
+
+  lazy val parserName = "StringVariableLengthInVariableWidthCharacters"
+  lazy val lengthText = e.length.constantAsString
+
+  // val maxBytes = Compiler.maxFieldContentLengthInBytes
+  //  var cbuf: CharBuffer = CharBuffer.allocate(0) // TODO: Performance: get a char buffer from a pool.
+  //  var cbufSize = 0
+}
+
+case class StringPatternMatched(e: ElementBase)
+  extends Terminal(e, true)
+  with WithParseErrorThrowing with TextReader with Padded {
+
+  val charset = e.knownEncodingCharset
+
+  def parser: DaffodilParser = new PrimParser(this, e) {
+    override def toString = "StringPatternMatched"
+    val pattern = e.lengthPattern
+
+    // The pattern will always be defined
+    lazy val dp = new DFDLDelimParserStatic(e.knownEncodingStringBitLengthFunction)
+    lazy val patternParser = dp.generateInputPatternedParser(pattern)
+
+    // TODO: Add parameter for changing CharBuffer size
+
+    val eName = e.toString()
+
+    def parse(start: PState): PState = withParseErrorThrowing(start) {
+      // withLoggingLevel(LogLevel.Info) 
+      {
+
+        log(LogLevel.Debug, "StringPatternMatched - %s - Parsing pattern at byte position: %s", eName, (start.bitPos >> 3))
+        log(LogLevel.Debug, "StringPatternMatched - %s - Parsing pattern at bit position: %s", eName, start.bitPos)
+
+        // some encodings aren't whole bytes.
+        // if (start.bitPos % 8 != 0) { return PE(start, "StringPatternMatched - not byte aligned.") }
+
+        val bytePos = (start.bitPos >> 3).toInt
+
+        log(LogLevel.Debug, "Retrieving reader")
+
+        val reader = getReader(charset, start.bitPos, start)
+
+        //        val d = new DelimParser(e.knownEncodingStringBitLengthFunction)
+        //        val result = d.parseInputPatterned(pattern, reader)
+        val result = dp.parseInputPatterned(patternParser, reader)
+
+        val postState = result match {
+          case _: DelimParseFailure => {
+            // TODO: Is this right? A no match constitutes zero length.  So Nil would need to be checked?
+            // Because we check for Nil first, this is valid and allowed.
+            PE(start, "%s: No match found!", this.toString())
+          }
+          case s: DelimParseSuccess => {
+            val endBitPos = start.bitPos + s.numBits
+            log(LogLevel.Debug, "StringPatternMatched - Parsed: %s", s.field)
+            log(LogLevel.Debug, "StringPatternMatched - Ended at bit position %s", endBitPos)
+
+            val endCharPos = if (start.charPos == -1) s.field.length() else start.charPos + s.field.length()
+            val currentElement = start.parentElement
+            currentElement.setDataValue(s.field)
+            start.withPos(endBitPos, endCharPos, Some(s.next))
+          }
+
+        }
+        postState
+      }
+    }
+  }
+
+  def unparser: Unparser = new Unparser(e) {
+    def unparse(start: UState): UState = {
+      Assert.notYetImplemented()
+    }
+  }
+}
+
+abstract class StringDelimited(e: ElementBase)
+  extends DelimParserBase(e, true)
+  with TextReader
+  with Padded
+  with WithParseErrorThrowing {
+  //
+  val es = e.optionEscapeScheme
+  val esObj = EscapeScheme.getEscapeScheme(es, e)
+  val tm = e.allTerminatingMarkup
+  val cname = toString
+
+  val eName = e.toString()
+  val charset = e.knownEncodingCharset
+  val elemBase = e
+
+  //val dp = new DFDLDelimParserStatic(e.knownEncodingStringBitLengthFunction)
+
+  // These static delims are used whether we're static or dynamic
+  // because even a dynamic can have some static from enclosing scopes.
+  //  val staticDelimsRaw = e.allTerminatingMarkup.filter(x => x.isConstant).map { _.constantAsString }
+  val staticDelimsRaw = e.allTerminatingMarkup.filter {
+    case (delimValue, _, _) => delimValue.isConstant
+  }.map {
+    case (delimValue, _, _) => delimValue.constantAsString
+  }
+  val staticDelimsCooked1 = staticDelimsRaw.map(raw => { new ListOfStringValueAsLiteral(raw.toString, e).cooked })
+  val staticDelimsCooked = staticDelimsCooked1.flatten
+  val (staticDelimsParser, staticDelimsRegex) = dp.generateDelimiter(staticDelimsCooked.toSet)
+  val combinedStaticDelimsParser = dp.combineLongest(staticDelimsParser)
+
+  //  def parseMethod(hasDelim: Boolean, delimsParser: dp.Parser[String], delimsRegex: Array[String],
+  //    reader: Reader[Char]): DelimParseResult
+
+  def parseMethod(hasDelim: Boolean, delimsParser: dp.Parser[String], delimsRegex: Array[String],
+    reader: Reader[Char]): DelimParseResult = {
+    // TODO: Change DFDLDelimParser calls to get rid of Array.empty[String] since we're only passing a single list the majority of the time.
+    if (esObj.escapeSchemeKind == EscapeSchemeKind.Block) {
+      val (escapeBlockParser, escapeBlockEndRegex, escapeEscapeRegex) = dp.generateEscapeBlockParsers2(delimsParser,
+        esObj.escapeBlockStart, esObj.escapeBlockEnd, esObj.escapeEscapeCharacter, justificationTrim, padChar, true)
+      val removeEscapeBlocksRegex = dp.removeEscapesBlocksRegex(escapeEscapeRegex, escapeBlockEndRegex)
+      val parseInputParser = dp.generateInputParser2(dp.emptyParser, delimsParser, Array.empty[String], delimsRegex,
+        hasDelim, justificationTrim, padChar, true)
+      dp.parseInputEscapeBlock(escapeBlockParser, dp.emptyParser, delimsParser, reader,
+        justificationTrim, removeEscapeBlocksRegex, parseInputParser)
+    } else if (esObj.escapeSchemeKind == EscapeSchemeKind.Character) {
+      val delimsRegexCombined = dp.combineDelimitersRegex(Array.empty[String], delimsRegex)
+      val escapeCharacterParser = dp.generateInputEscapeCharacterParser2(delimsParser, delimsRegexCombined,
+        hasDelim, esObj.escapeCharacter, esObj.escapeEscapeCharacter, justificationTrim, padChar, true)
+      val esRegex = dp.convertDFDLLiteralToRegex(esObj.escapeCharacter)
+      val esEsRegex = dp.convertDFDLLiteralToRegex(esObj.escapeEscapeCharacter)
+      val removeEscapeCharacterRegex = dp.generateRemoveEscapeCharactersSameRegex(esRegex)
+      val removeUnescapedEscapesRegex = dp.removeUnescapedEscapesRegex(esEsRegex, esRegex)
+      val removeEscapeEscapesThatEscapeRegex = dp.removeEscapeEscapesThatEscapeRegex(esEsRegex, esRegex)
+      val removeEscapeRegex = dp.removeEscapeRegex(esRegex)
+      dp.parseInputEscapeCharacter(escapeCharacterParser, dp.emptyParser, delimsParser, reader,
+        justificationTrim, removeEscapeCharacterRegex, removeUnescapedEscapesRegex,
+        removeEscapeEscapesThatEscapeRegex, removeEscapeRegex, esObj.escapeCharacter,
+        esObj.escapeEscapeCharacter)
+    } else {
+      //d.parseInput(Set.empty[String], delimsCooked.toSet, reader, justificationTrim, padChar)
+      val parseInputParser = dp.generateInputParser2(dp.emptyParser, delimsParser, Array.empty[String], delimsRegex,
+        hasDelim, justificationTrim, padChar, true)
+      dp.parseInput(parseInputParser, dp.emptyParser, delimsParser, reader, justificationTrim)
+    }
+
+  }
+
+  def getDelims(pstate: PState): (List[String], Array[String], dp.Parser[String], Option[VariableMap])
+
+  /**
+   * Called at compile time in static case, at runtime for dynamic case.
+   */
+  def errorIfDelimsHaveWSPStar(delims: List[String]): Unit = {
+    if (delims.filter(x => x == "%WSP*;").length > 0) {
+      // We cannot detect this error until expressions have been evaluated!
+      log(LogLevel.Debug, "%s - Failed due to WSP* detected as a delimiter for lengthKind=delimited.", eName)
+      elemBase.schemaDefinitionError("WSP* cannot be used as a delimiter when lengthKind=delimited.")
+    }
+  }
+
+  def processResult(result: DelimParseResult, state: PState): PState = {
+    result match {
+      case f: DelimParseFailure =>
+        return parser.PE(state, "%s - %s - Parse failed.", this.toString(), eName)
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val numBits = s.numBits
+        log(LogLevel.Debug, "%s - Parsed: %s Parsed Bytes: %s (bits %s)", eName, field, numBits / 8, numBits)
+        val endCharPos = if (state.charPos == -1) s.numCharsRead else state.charPos + s.numCharsRead
+        val endBitPos = state.bitPos + numBits
+        val currentElement = state.parentElement
+        currentElement.setDataValue(field)
+        return state.withPos(endBitPos, endCharPos, Some(s.next))
+      }
+    }
+  }
+
+  def parser: DaffodilParser = new PrimParser(this, e) {
+    //    override def toString = cname + "(" + tm.map { _.prettyExpr } + ")"
+    override def toString = cname + "(" + tm.map { case (delimValue, _, _) => delimValue.prettyExpr } + ")"
+
+    def parse(start: PState): PState = withParseErrorThrowing(start) {
+
+      val (delimsCooked, delimsRegex, delimsParser, vars) = getDelims(start)
+
+      // We must feed variable context out of one evaluation and into the next.
+      // So that the resulting variable map has the updated status of all evaluated variables.
+      val postEvalState = vars match {
+        case Some(v) => start.withVariables(v)
+        case None => start
+      }
+
+      log(LogLevel.Debug, "%s - Looking for: %s Count: %s", eName, delimsCooked, delimsCooked.length)
+
+      val bytePos = (postEvalState.bitPos >> 3).toInt
+      log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, postEvalState.bitPos)
+      log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)
+
+      val reader = getReader(charset, postEvalState.bitPos, postEvalState)
+      val hasDelim = delimsCooked.length > 0
+
+      val result = try {
+        parseMethod(hasDelim, delimsParser, delimsRegex, reader)
+      } catch {
+        case mie: MalformedInputException =>
+          throw new ParseError(e, Some(postEvalState), "Malformed input, length: %s", mie.getInputLength())
+      }
+      processResult(result, postEvalState)
+    }
+  }
+
+  def unparser: Unparser = new Unparser(e) {
+    //    override def toString = cname + "(" + tm.map { _.prettyExpr } + ")"
+    override def toString = cname + "(" + tm.map { case (delimValue, _, _) => delimValue.prettyExpr } + ")"
+
+    def unparse(start: UState): UState =
+      // withLoggingLevel(LogLevel.Info) 
+      {
+        val data = start.currentElement.getText
+
+        val encoder = charset.newEncoder()
+        start.outStream.setEncoder(encoder)
+        start.outStream.fillCharBuffer(data)
+        log(LogLevel.Debug, "Unparsed: " + start.outStream.getData)
+        start
+      }
+  }
+}
+
+trait StaticDelim { self: StringDelimited =>
+
+  // do this at creation time if we're static
+  errorIfDelimsHaveWSPStar(staticDelimsCooked)
+
+  def getDelims(pstate: PState): (List[String], Array[String], dp.Parser[String], Option[VariableMap]) = {
+    (staticDelimsCooked, staticDelimsRegex, combinedStaticDelimsParser, None)
+  }
+
+}
+
+case class StringDelimitedEndOfDataStatic(e: ElementBase)
+  extends StringDelimited(e) with StaticDelim
+
+trait DynamicDelim { self: StringDelimited =>
+
+  override def getDelims(pstate: PState): (List[String], Array[String], dp.Parser[String], Option[VariableMap]) = {
+    // We must feed variable context out of one evaluation and into the next.
+    // So that the resulting variable map has the updated status of all evaluated variables.
+    var vars = pstate.variableMap
+
+    val dynamicDelimsRaw = elemBase.allTerminatingMarkup.filter { case (delimValue, _, _) => !delimValue.isConstant }.map {
+      case (delimValue, _, _) =>
+        {
+          val R(res, newVMap) = delimValue.evaluate(pstate.parentElement, vars, pstate)
+          vars = newVMap
+          res
+        }
+    }
+    val dynamicDelimsCooked1 = dynamicDelimsRaw.map(raw => { new ListOfStringValueAsLiteral(raw.toString, elemBase).cooked })
+    val dynamicDelimsCooked = dynamicDelimsCooked1.flatten
+    val (dynamicDelimsParser, dynamicDelimsRegex) = dp.generateDelimiter(dynamicDelimsCooked.toSet)
+
+    // Combine dynamic and with static delims if they exist
+    val delimsParser = dp.combineLongest(dynamicDelimsParser.union(staticDelimsParser))
+    val delimsCooked = dynamicDelimsCooked.union(staticDelimsCooked)
+    val delimsRegex = dynamicDelimsRegex.union(staticDelimsRegex)
+    //
+    // moved check here to avoid need for another abstract method to 
+    // factor this out.
+    errorIfDelimsHaveWSPStar(delimsCooked)
+
+    (delimsCooked, delimsRegex, delimsParser, Some(vars))
+  }
+
+}
+
+case class StringDelimitedEndOfDataDynamic(e: ElementBase)
+  extends StringDelimited(e) with DynamicDelim
+
+abstract class HexBinaryDelimited(e: ElementBase) extends StringDelimited(e) {
+  override val charset: Charset = Charset.forName("ISO-8859-1")
+  override def processResult(result: DelimParseResult, state: PState): PState = {
+    result match {
+      case f: DelimParseFailure =>
+        return parser.PE(state, "%s - %s - Parse failed.", this.toString(), eName)
+      case s: DelimParseSuccess => {
+        val field = s.get
+        val numBits = s.numBits
+        log(LogLevel.Debug, "%s - Parsed: %s Parsed Bytes: %s (bits %s)", eName, field, numBits / 8, numBits)
+        val endCharPos = if (state.charPos == -1) s.numCharsRead else state.charPos + s.numCharsRead
+        val endBitPos = state.bitPos + numBits
+        val currentElement = state.parentElement
+        val hexStr = field.map(c => c.toByte.formatted("%02X")).mkString
+        currentElement.setDataValue(hexStr)
+        return state.withPos(endBitPos, endCharPos, Some(s.next))
+      }
+    }
+  }
+}
+
+case class HexBinaryDelimitedEndOfDataStatic(e: ElementBase)
+  extends HexBinaryDelimited(e) with StaticDelim
+
+case class HexBinaryDelimitedEndOfDataDynamic(e: ElementBase)
+  extends HexBinaryDelimited(e) with DynamicDelim
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/SpecifiedLength.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/SpecifiedLength.scala
index 89b1875..4e56b73 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/SpecifiedLength.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/SpecifiedLength.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.processors

-

+package edu.illinois.ncsa.daffodil.processors
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,334 +30,334 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import edu.illinois.ncsa.daffodil.dsom.DFDLAssert

-import edu.illinois.ncsa.daffodil.dsom.DFDLDiscriminator

-import edu.illinois.ncsa.daffodil.dsom.DFDLSetVariable

-import edu.illinois.ncsa.daffodil.dsom.ElementBase

-import edu.illinois.ncsa.daffodil.grammar.Gram

-import edu.illinois.ncsa.daffodil.grammar.NamedGram

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TestKind

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import edu.illinois.ncsa.daffodil.dsom.ElementBase

-import edu.illinois.ncsa.daffodil.exceptions.UnsuppressableException

-import edu.illinois.ncsa.daffodil.dsom.R

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextNumberJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextTrimKind

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextBooleanJustification

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextCalendarJustification

-import edu.illinois.ncsa.daffodil.dsom.ElementBase

-import edu.illinois.ncsa.daffodil.grammar.Terminal

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.dsom.DiagnosticUtils._

-

-abstract class SpecifiedLengthCombinatorBase(val e: ElementBase, eGram: => Gram)

-  extends Terminal(e, true)

-  with RuntimeExplicitLengthMixin[Long] {

-  //  extends NamedGram(e) {

-

-  // requiredEvaluations(eGram) // Note: not really required for grammar objects. 

-  // The eGram is only required if the grammar clause actually ends up spliced 

-  // into the final grammar, and we can't tell that here, so whether the egram

-  // ends up evaluated or not really has to happen in the application logic.

-

-  val eParser = eGram.parser

-

-  def kind: String

-

-  def toBriefXML(depthLimit: Int = -1): String = {

-    if (depthLimit == 0) "..." else

-      "<SpecifiedLength" + kind + ">" +

-        eParser.toBriefXML(depthLimit - 1) +

-        "</SpecifiedLength" + kind + ">"

-  }

-

-}

-

-class SpecifiedLengthPattern(e: ElementBase, eGram: => Gram)

-  extends SpecifiedLengthCombinatorBase(e, eGram) {

-

-  val kind = "Pattern"

-

-  def parser: Parser = new SpecifiedLengthPatternParser(this, e)

-  def unparser: Unparser = new DummyUnparser(e)

-

-}

-

-class SpecifiedLengthExplicitBitsFixed(e: ElementBase, eGram: => Gram, nBits: Long)

-  extends SpecifiedLengthCombinatorBase(e, eGram) {

-

-  val kind = "ExplicitBitsFixed"

-

-  def parser: Parser = new SpecifiedLengthExplicitBitsFixedParser(this, e, nBits)

-  def unparser: Unparser = new DummyUnparser(e)

-

-}

-

-class SpecifiedLengthExplicitBits(e: ElementBase, eGram: => Gram)

-  extends SpecifiedLengthCombinatorBase(e, eGram) {

-

-  val kind = "ExplicitBits"

-

-  def parser: Parser = new SpecifiedLengthExplicitBitsParser(this, e)

-  def unparser: Unparser = new DummyUnparser(e)

-

-}

-

-class SpecifiedLengthExplicitBytesFixed(e: ElementBase, eGram: => Gram, nBytes: Long)

-  extends SpecifiedLengthCombinatorBase(e, eGram) {

-

-  val kind = "ExplicitBytesFixed"

-

-  def parser: Parser = new SpecifiedLengthExplicitBytesFixedParser(this, e, nBytes)

-  def unparser: Unparser = new DummyUnparser(e)

-

-}

-

-class SpecifiedLengthExplicitBytes(e: ElementBase, eGram: => Gram)

-  extends SpecifiedLengthCombinatorBase(e, eGram) {

-

-  val kind = "ExplicitBytes"

-

-  def parser: Parser = new SpecifiedLengthExplicitBytesParser(this, e)

-  def unparser: Unparser = new DummyUnparser(e)

-

-}

-

-class SpecifiedLengthExplicitCharactersFixed(e: ElementBase, eGram: => Gram, nChars: Long)

-  extends SpecifiedLengthCombinatorBase(e, eGram) {

-

-  val kind = "ExplicitCharactersFixed"

-

-  def parser: Parser = new SpecifiedLengthExplicitCharactersFixedParser(this, e, nChars)

-  def unparser: Unparser = new DummyUnparser(e)

-

-}

-

-class SpecifiedLengthExplicitCharacters(e: ElementBase, eGram: => Gram)

-  extends SpecifiedLengthCombinatorBase(e, eGram) {

-

-  val kind = "ExplicitCharacters"

-

-  def parser: Parser = new SpecifiedLengthExplicitCharactersParser(this, e)

-  def unparser: Unparser = new DummyUnparser(e)

-

-}

-

-abstract class SpecifiedLengthParserBase(combinator: SpecifiedLengthCombinatorBase,

-  e: ElementBase)

-  extends PrimParser(combinator, e)

-  with WithParseErrorThrowing {

-

-  def toBriefXML = combinator.toBriefXML _

-

-  final def parse(pstate: PState, endBitPos: Long, e: ElementBase) = {

-    log(LogLevel.Debug, "Limiting data to %s bits.", endBitPos)

-    val postState1 = pstate.withEndBitLimit(endBitPos)

-    val postState2 = combinator.eParser.parse1(postState1, e)

-    

-    log(LogLevel.Debug, "Restoring data limit to %s bits.", pstate.bitLimit)

-    

-    val postState3 = postState2.withEndBitLimit(pstate.bitLimit)

-    val finalState = postState3.status match {

-      case Success => {

-        // Check that the parsed length is less than or equal to the length of the parent

-        //Assert.invariant(postState2.bitPos <= endBitPos)

-        this.PECheck(postState2.bitPos <= endBitPos, "The parsed length of the children was greater than that of the parent.")

-        postState3.withPos(endBitPos, -1, None)

-      }

-      case _ => postState3

-    }

-    finalState

-  }

-

-}

-

-class SpecifiedLengthPatternParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase)

-  extends SpecifiedLengthParserBase(combinator, e) {

-

-  val charset = e.knownEncodingCharset

-  val pattern = e.lengthPattern

-  

-  if(!e.isScannable) e.SDE("Element %s does not meet the requirements of Pattern-Based lengths and Scanability.\nThe element and its children must be representation='text' and share the same encoding.", e.prettyName)

-

-  def parse(start: PState): PState = withParseErrorThrowing(start) {

-    val in = start.inStream

-

-    val reader = in.getCharReader(charset, start.bitPos)

-    val d = new DelimParser(e.knownEncodingStringBitLengthFunction)

-    val result = d.parseInputPatterned(pattern, reader)

-    val endBitPos =

-      result match {

-        case _: DelimParseFailure => start.bitPos + 0 // no match == length is zero!

-        case s: DelimParseSuccess => start.bitPos + s.numBits

-      }

-    val postEState = parse(start, endBitPos, e)

-    postEState

-  }

-}

-

-class SpecifiedLengthExplicitBitsParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase)

-  extends SpecifiedLengthParserBase(combinator, e) {

-

-  val charset = e.knownEncodingCharset

-  val expr = e.length

-

-  def parse(start: PState): PState = withParseErrorThrowing(start) {

-

-    val (pState, nBits) = combinator.getBitLength(start)

-    val in = pState.inStream

-

-    try {

-      val nBytes = scala.math.ceil(nBits / 8.0).toLong

-      val bytes = in.getBytes(pState.bitPos, nBytes)

-      val endBitPos = pState.bitPos + nBits

-      val postEState = parse(pState, endBitPos, e)

-      return postEState

-    } catch {

-      case ex: IndexOutOfBoundsException => {

-        // Insufficient bytes in field, but we need to still allow processing

-        // to test for Nils

-        val endBitPos = start.bitPos + 0

-        val postEState = parse(start, endBitPos, e)

-        return postEState

-      }

-      case u: UnsuppressableException => throw u

-      case e: Exception => { return PE(pState, "SpecifiedLengthExplicitBitsParser - Exception: \n%s", e.getStackTraceString) }

-    }

-  }

-}

-

-class SpecifiedLengthExplicitBitsFixedParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase, nBits: Long)

-  extends SpecifiedLengthParserBase(combinator, e) {

-

-  val charset = e.knownEncodingCharset

-

-  def parse(start: PState): PState = withParseErrorThrowing(start) {

-

-    val in = start.inStream

-

-    try {

-      val nBytes = scala.math.ceil(nBits / 8.0).toLong

-      val bytes = in.getBytes(start.bitPos, nBytes)

-      val endBitPos = start.bitPos + nBits

-      val postEState = parse(start, endBitPos, e)

-      return postEState

-    } catch {

-      case ex: IndexOutOfBoundsException => {

-        // Insufficient bits in field, but we need to still allow processing

-        // to test for Nils

-        val endBitPos = start.bitPos + 0

-        val postEState = parse(start, endBitPos, e)

-        return postEState

-      }

-      case u: UnsuppressableException => throw u

-      case ex: Exception => { return PE(start, "SpecifiedLengthExplicitBitsFixedParser - Exception: \n%s", ex.getStackTraceString) }

-    }

-  }

-}

-

-class SpecifiedLengthExplicitBytesParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase)

-  extends SpecifiedLengthParserBase(combinator, e) {

-

-  val charset = e.knownEncodingCharset

-  val expr = e.length

-

-  def parse(start: PState): PState = withParseErrorThrowing(start) {

-

-    val (pState, nBytes) = combinator.getLength(start)

-    val in = pState.inStream

-

-    try {

-      val bytes = in.getBytes(pState.bitPos, nBytes)

-      val endBitPos = pState.bitPos + (nBytes * 8)

-      val postEState = parse(pState, endBitPos, e)

-      return postEState

-    } catch {

-      case ex: IndexOutOfBoundsException => {

-        // Insufficient bytes in field, but we need to still allow processing

-        // to test for Nils

-        val endBitPos = start.bitPos + 0

-        val postEState = parse(start, endBitPos, e)

-        return postEState

-      }

-      case u: UnsuppressableException => throw u

-      case ex: Exception => { return PE(pState, "SpecifiedLengthExplicitBytesParser - Exception: \n%s", ex.getStackTraceString) }

-    }

-  }

-}

-

-class SpecifiedLengthExplicitBytesFixedParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase, nBytes: Long)

-  extends SpecifiedLengthParserBase(combinator, e) {

-

-  val charset = e.knownEncodingCharset

-

-  def parse(start: PState): PState = withParseErrorThrowing(start) {

-

-    val in = start.inStream

-

-    try {

-      // val bytes = in.getBytes(start.bitPos, nBytes)

-      val endBitPos = start.bitPos + (nBytes * 8)

-      val postEState = super.parse(start, endBitPos, e)

-      return postEState

-    } catch {

-      case ex: IndexOutOfBoundsException => {

-        // Insufficient bytes in field, but we need to still allow processing

-        // to test for Nils

-        val endBitPos = start.bitPos + 0

-        val postEState = super.parse(start, endBitPos, e)

-        return postEState

-      }

-      case u: UnsuppressableException => throw u

-      case ex: Exception => { return PE(start, "SpecifiedLengthExplicitBytesFixedParser - Exception: \n%s", ex.getStackTraceString) }

-    }

-  }

-}

-

-class SpecifiedLengthExplicitCharactersFixedParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase, nChars: Long)

-  extends SpecifiedLengthParserBase(combinator, e) {

-

-  val charset = e.knownEncodingCharset

-

-  def parse(start: PState): PState = withParseErrorThrowing(start) {

-

-    val in = start.inStream

-    val rdr = in.getCharReader(charset, start.bitPos)

-    val d = new DelimParser(e.knownEncodingStringBitLengthFunction)

-    val result = d.parseInputNCharacters(nChars, rdr, TextJustificationType.None, "")

-    val endBitPos =

-      result match {

-        case _: DelimParseFailure => start.bitPos + 0 // no match == length is zero!

-        case s: DelimParseSuccess => start.bitPos + s.numBits

-      }

-    val postEState = parse(start, endBitPos, e)

-    postEState

-  }

-}

-

-class SpecifiedLengthExplicitCharactersParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase)

-  extends SpecifiedLengthParserBase(combinator, e) {

-

-  val charset = e.knownEncodingCharset

-  val expr = e.length

-

-  def parse(start: PState): PState = withParseErrorThrowing(start) {

-

-    val (pState, nChars) = combinator.getLength(start)

-    val in = pState.inStream

-    val rdr = in.getCharReader(charset, pState.bitPos)

-    val d = new DelimParser(e.knownEncodingStringBitLengthFunction)

-    val result = d.parseInputNCharacters(nChars, rdr, TextJustificationType.None, "")

-    val endBitPos =

-      result match {

-        case _: DelimParseFailure => pState.bitPos + 0 // no match == length is zero!

-        case s: DelimParseSuccess => pState.bitPos + s.numBits

-      }

-    val postEState = parse(pState, endBitPos, e)

-    postEState

-  }

-}

-

+ */
+
+import edu.illinois.ncsa.daffodil.dsom.DFDLAssert
+import edu.illinois.ncsa.daffodil.dsom.DFDLDiscriminator
+import edu.illinois.ncsa.daffodil.dsom.DFDLSetVariable
+import edu.illinois.ncsa.daffodil.dsom.ElementBase
+import edu.illinois.ncsa.daffodil.grammar.Gram
+import edu.illinois.ncsa.daffodil.grammar.NamedGram
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TestKind
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import edu.illinois.ncsa.daffodil.dsom.ElementBase
+import edu.illinois.ncsa.daffodil.exceptions.UnsuppressableException
+import edu.illinois.ncsa.daffodil.dsom.R
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextNumberJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextTrimKind
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextStringJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextBooleanJustification
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TextCalendarJustification
+import edu.illinois.ncsa.daffodil.dsom.ElementBase
+import edu.illinois.ncsa.daffodil.grammar.Terminal
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.dsom.DiagnosticUtils._
+
+abstract class SpecifiedLengthCombinatorBase(val e: ElementBase, eGram: => Gram)
+  extends Terminal(e, true)
+  with RuntimeExplicitLengthMixin[Long] {
+  //  extends NamedGram(e) {
+
+  // requiredEvaluations(eGram) // Note: not really required for grammar objects. 
+  // The eGram is only required if the grammar clause actually ends up spliced 
+  // into the final grammar, and we can't tell that here, so whether the egram
+  // ends up evaluated or not really has to happen in the application logic.
+
+  val eParser = eGram.parser
+
+  def kind: String
+
+  def toBriefXML(depthLimit: Int = -1): String = {
+    if (depthLimit == 0) "..." else
+      "<SpecifiedLength" + kind + ">" +
+        eParser.toBriefXML(depthLimit - 1) +
+        "</SpecifiedLength" + kind + ">"
+  }
+
+}
+
+class SpecifiedLengthPattern(e: ElementBase, eGram: => Gram)
+  extends SpecifiedLengthCombinatorBase(e, eGram) {
+
+  val kind = "Pattern"
+
+  def parser: Parser = new SpecifiedLengthPatternParser(this, e)
+  def unparser: Unparser = new DummyUnparser(e)
+
+}
+
+class SpecifiedLengthExplicitBitsFixed(e: ElementBase, eGram: => Gram, nBits: Long)
+  extends SpecifiedLengthCombinatorBase(e, eGram) {
+
+  val kind = "ExplicitBitsFixed"
+
+  def parser: Parser = new SpecifiedLengthExplicitBitsFixedParser(this, e, nBits)
+  def unparser: Unparser = new DummyUnparser(e)
+
+}
+
+class SpecifiedLengthExplicitBits(e: ElementBase, eGram: => Gram)
+  extends SpecifiedLengthCombinatorBase(e, eGram) {
+
+  val kind = "ExplicitBits"
+
+  def parser: Parser = new SpecifiedLengthExplicitBitsParser(this, e)
+  def unparser: Unparser = new DummyUnparser(e)
+
+}
+
+class SpecifiedLengthExplicitBytesFixed(e: ElementBase, eGram: => Gram, nBytes: Long)
+  extends SpecifiedLengthCombinatorBase(e, eGram) {
+
+  val kind = "ExplicitBytesFixed"
+
+  def parser: Parser = new SpecifiedLengthExplicitBytesFixedParser(this, e, nBytes)
+  def unparser: Unparser = new DummyUnparser(e)
+
+}
+
+class SpecifiedLengthExplicitBytes(e: ElementBase, eGram: => Gram)
+  extends SpecifiedLengthCombinatorBase(e, eGram) {
+
+  val kind = "ExplicitBytes"
+
+  def parser: Parser = new SpecifiedLengthExplicitBytesParser(this, e)
+  def unparser: Unparser = new DummyUnparser(e)
+
+}
+
+class SpecifiedLengthExplicitCharactersFixed(e: ElementBase, eGram: => Gram, nChars: Long)
+  extends SpecifiedLengthCombinatorBase(e, eGram) {
+
+  val kind = "ExplicitCharactersFixed"
+
+  def parser: Parser = new SpecifiedLengthExplicitCharactersFixedParser(this, e, nChars)
+  def unparser: Unparser = new DummyUnparser(e)
+
+}
+
+class SpecifiedLengthExplicitCharacters(e: ElementBase, eGram: => Gram)
+  extends SpecifiedLengthCombinatorBase(e, eGram) {
+
+  val kind = "ExplicitCharacters"
+
+  def parser: Parser = new SpecifiedLengthExplicitCharactersParser(this, e)
+  def unparser: Unparser = new DummyUnparser(e)
+
+}
+
+abstract class SpecifiedLengthParserBase(combinator: SpecifiedLengthCombinatorBase,
+  e: ElementBase)
+  extends PrimParser(combinator, e)
+  with WithParseErrorThrowing {
+
+  def toBriefXML = combinator.toBriefXML _
+
+  final def parse(pstate: PState, endBitPos: Long, e: ElementBase) = {
+    log(LogLevel.Debug, "Limiting data to %s bits.", endBitPos)
+    val postState1 = pstate.withEndBitLimit(endBitPos)
+    val postState2 = combinator.eParser.parse1(postState1, e)
+    
+    log(LogLevel.Debug, "Restoring data limit to %s bits.", pstate.bitLimit)
+    
+    val postState3 = postState2.withEndBitLimit(pstate.bitLimit)
+    val finalState = postState3.status match {
+      case Success => {
+        // Check that the parsed length is less than or equal to the length of the parent
+        //Assert.invariant(postState2.bitPos <= endBitPos)
+        this.PECheck(postState2.bitPos <= endBitPos, "The parsed length of the children was greater than that of the parent.")
+        postState3.withPos(endBitPos, -1, None)
+      }
+      case _ => postState3
+    }
+    finalState
+  }
+
+}
+
+class SpecifiedLengthPatternParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase)
+  extends SpecifiedLengthParserBase(combinator, e) {
+
+  val charset = e.knownEncodingCharset
+  val pattern = e.lengthPattern
+  
+  if(!e.isScannable) e.SDE("Element %s does not meet the requirements of Pattern-Based lengths and Scanability.\nThe element and its children must be representation='text' and share the same encoding.", e.prettyName)
+
+  def parse(start: PState): PState = withParseErrorThrowing(start) {
+    val in = start.inStream
+
+    val reader = in.getCharReader(charset, start.bitPos)
+    val d = new DelimParser(e.knownEncodingStringBitLengthFunction)
+    val result = d.parseInputPatterned(pattern, reader)
+    val endBitPos =
+      result match {
+        case _: DelimParseFailure => start.bitPos + 0 // no match == length is zero!
+        case s: DelimParseSuccess => start.bitPos + s.numBits
+      }
+    val postEState = parse(start, endBitPos, e)
+    postEState
+  }
+}
+
+class SpecifiedLengthExplicitBitsParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase)
+  extends SpecifiedLengthParserBase(combinator, e) {
+
+  val charset = e.knownEncodingCharset
+  val expr = e.length
+
+  def parse(start: PState): PState = withParseErrorThrowing(start) {
+
+    val (pState, nBits) = combinator.getBitLength(start)
+    val in = pState.inStream
+
+    try {
+      val nBytes = scala.math.ceil(nBits / 8.0).toLong
+      val bytes = in.getBytes(pState.bitPos, nBytes)
+      val endBitPos = pState.bitPos + nBits
+      val postEState = parse(pState, endBitPos, e)
+      return postEState
+    } catch {
+      case ex: IndexOutOfBoundsException => {
+        // Insufficient bytes in field, but we need to still allow processing
+        // to test for Nils
+        val endBitPos = start.bitPos + 0
+        val postEState = parse(start, endBitPos, e)
+        return postEState
+      }
+      case u: UnsuppressableException => throw u
+      case e: Exception => { return PE(pState, "SpecifiedLengthExplicitBitsParser - Exception: \n%s", e.getStackTraceString) }
+    }
+  }
+}
+
+class SpecifiedLengthExplicitBitsFixedParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase, nBits: Long)
+  extends SpecifiedLengthParserBase(combinator, e) {
+
+  val charset = e.knownEncodingCharset
+
+  def parse(start: PState): PState = withParseErrorThrowing(start) {
+
+    val in = start.inStream
+
+    try {
+      val nBytes = scala.math.ceil(nBits / 8.0).toLong
+      val bytes = in.getBytes(start.bitPos, nBytes)
+      val endBitPos = start.bitPos + nBits
+      val postEState = parse(start, endBitPos, e)
+      return postEState
+    } catch {
+      case ex: IndexOutOfBoundsException => {
+        // Insufficient bits in field, but we need to still allow processing
+        // to test for Nils
+        val endBitPos = start.bitPos + 0
+        val postEState = parse(start, endBitPos, e)
+        return postEState
+      }
+      case u: UnsuppressableException => throw u
+      case ex: Exception => { return PE(start, "SpecifiedLengthExplicitBitsFixedParser - Exception: \n%s", ex.getStackTraceString) }
+    }
+  }
+}
+
+class SpecifiedLengthExplicitBytesParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase)
+  extends SpecifiedLengthParserBase(combinator, e) {
+
+  val charset = e.knownEncodingCharset
+  val expr = e.length
+
+  def parse(start: PState): PState = withParseErrorThrowing(start) {
+
+    val (pState, nBytes) = combinator.getLength(start)
+    val in = pState.inStream
+
+    try {
+      val bytes = in.getBytes(pState.bitPos, nBytes)
+      val endBitPos = pState.bitPos + (nBytes * 8)
+      val postEState = parse(pState, endBitPos, e)
+      return postEState
+    } catch {
+      case ex: IndexOutOfBoundsException => {
+        // Insufficient bytes in field, but we need to still allow processing
+        // to test for Nils
+        val endBitPos = start.bitPos + 0
+        val postEState = parse(start, endBitPos, e)
+        return postEState
+      }
+      case u: UnsuppressableException => throw u
+      case ex: Exception => { return PE(pState, "SpecifiedLengthExplicitBytesParser - Exception: \n%s", ex.getStackTraceString) }
+    }
+  }
+}
+
+class SpecifiedLengthExplicitBytesFixedParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase, nBytes: Long)
+  extends SpecifiedLengthParserBase(combinator, e) {
+
+  val charset = e.knownEncodingCharset
+
+  def parse(start: PState): PState = withParseErrorThrowing(start) {
+
+    val in = start.inStream
+
+    try {
+      // val bytes = in.getBytes(start.bitPos, nBytes)
+      val endBitPos = start.bitPos + (nBytes * 8)
+      val postEState = super.parse(start, endBitPos, e)
+      return postEState
+    } catch {
+      case ex: IndexOutOfBoundsException => {
+        // Insufficient bytes in field, but we need to still allow processing
+        // to test for Nils
+        val endBitPos = start.bitPos + 0
+        val postEState = super.parse(start, endBitPos, e)
+        return postEState
+      }
+      case u: UnsuppressableException => throw u
+      case ex: Exception => { return PE(start, "SpecifiedLengthExplicitBytesFixedParser - Exception: \n%s", ex.getStackTraceString) }
+    }
+  }
+}
+
+class SpecifiedLengthExplicitCharactersFixedParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase, nChars: Long)
+  extends SpecifiedLengthParserBase(combinator, e) {
+
+  val charset = e.knownEncodingCharset
+
+  def parse(start: PState): PState = withParseErrorThrowing(start) {
+
+    val in = start.inStream
+    val rdr = in.getCharReader(charset, start.bitPos)
+    val d = new DelimParser(e.knownEncodingStringBitLengthFunction)
+    val result = d.parseInputNCharacters(nChars, rdr, TextJustificationType.None, "")
+    val endBitPos =
+      result match {
+        case _: DelimParseFailure => start.bitPos + 0 // no match == length is zero!
+        case s: DelimParseSuccess => start.bitPos + s.numBits
+      }
+    val postEState = parse(start, endBitPos, e)
+    postEState
+  }
+}
+
+class SpecifiedLengthExplicitCharactersParser(combinator: SpecifiedLengthCombinatorBase, e: ElementBase)
+  extends SpecifiedLengthParserBase(combinator, e) {
+
+  val charset = e.knownEncodingCharset
+  val expr = e.length
+
+  def parse(start: PState): PState = withParseErrorThrowing(start) {
+
+    val (pState, nChars) = combinator.getLength(start)
+    val in = pState.inStream
+    val rdr = in.getCharReader(charset, pState.bitPos)
+    val d = new DelimParser(e.knownEncodingStringBitLengthFunction)
+    val result = d.parseInputNCharacters(nChars, rdr, TextJustificationType.None, "")
+    val endBitPos =
+      result match {
+        case _: DelimParseFailure => pState.bitPos + 0 // no match == length is zero!
+        case s: DelimParseSuccess => pState.bitPos + s.numBits
+      }
+    val postEState = parse(pState, endBitPos, e)
+    postEState
+  }
+}
+
diff --git a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/StatementParsers.scala b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/StatementParsers.scala
index 3e0d30b..32849e9 100644
--- a/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/StatementParsers.scala
+++ b/daffodil-core/src/main/scala/edu/illinois/ncsa/daffodil/processors/StatementParsers.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.processors

-

+package edu.illinois.ncsa.daffodil.processors
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,156 +30,156 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import edu.illinois.ncsa.daffodil.dsom.DFDLAssert

-import edu.illinois.ncsa.daffodil.dsom.DFDLDiscriminator

-import edu.illinois.ncsa.daffodil.dsom.DFDLSetVariable

-import edu.illinois.ncsa.daffodil.dsom.ElementBase

-import edu.illinois.ncsa.daffodil.grammar.Gram

-import edu.illinois.ncsa.daffodil.grammar.NamedGram

-import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TestKind

-import edu.illinois.ncsa.daffodil.exceptions.Assert

-import edu.illinois.ncsa.daffodil.dsom.DiagnosticUtils._

-

-object StmtEval {

-  def apply(context: ElementBase, eGram: Gram) = {

-    // little optimization here. If there are no statements (most common case), then let's 

-    // shortcut and just use the guts parser.

-    val hasStatements = context.notNewVariableInstanceStatements.size > 0

-    if (hasStatements)

-      new StmtEval(context, eGram)

-    else

-      eGram

-  }

-}

-

-class StmtEval(context: ElementBase, eGram: Gram)

-  extends NamedGram(context) {

-

-  // The order of things matters in some cases, so to be consistent we'll always use the 

-  // same order even when it doesn't matter

-

-  // The order of evaluation of statements is:

-  // - pattern discriminators

-  // - pattern asserts

-  // - the parsing of the element itself

-  // - setVariables

-  // - test discriminators (must be attempted even if the parsing of element or setVariable statements fail)

-  // - test asserts

-

-  // requiredEvaluations(patDiscrim, patAssert, eGram, setVar, testDiscrim, testAssert)

-  // Note: above not needed as these are ALWAYS evaluated below.

-

-  val patDiscrim = context.discriminatorStatements.filter(_.testKind == TestKind.Pattern).map(_.gram)

-  val patAssert = context.assertStatements.filter(_.testKind == TestKind.Pattern).map(_.gram)

-  val setVar = context.setVariableStatements.map(_.gram)

-  val testDiscrim = context.discriminatorStatements.filter(_.testKind == TestKind.Expression).map(_.gram)

-  val testAssert = context.assertStatements.filter(_.testKind == TestKind.Expression).map(_.gram)

-

-  val eParser = eGram.parser

-

-  def parser: Parser = new StatementElementParser(context)

-

-  class StatementElementParser(context: ElementBase) extends PrimParser(this, context) {

-

-    Assert.invariant(context.notNewVariableInstanceStatements.size > 0)

-    Assert.invariant(testDiscrim.size <= 1)

-    Assert.invariant(patDiscrim.size <= 1)

-

-    override def toBriefXML(depthLimit: Int = -1): String = {

-      if (depthLimit == 0) "..." else

-        "<StmtEval>" +

-          patDiscrim.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +

-          patAssert.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +

-          eParser.toBriefXML(depthLimit - 1) +

-          setVar.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +

-          testDiscrim.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +

-          testAssert.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +

-          "</StmtEval>"

-    }

-

-    def parse(pstate: PState): PState = {

-      //Removed checks now done at compilation

-

-      var afterPatDisc = pstate //we're first so don't do .withPos(pstate.bitPos, pstate.charPos)

-      patDiscrim.map(_.parser).foreach(d => {

-        afterPatDisc = d.parse1(afterPatDisc, context)

-        // Pattern fails at the start of the Element

-        if (afterPatDisc.status != Success) { return afterPatDisc }

-      })

-

-      // now here we backup and run the pattern Asserts 

-      // against the data at the start of the element's representation again.

-      var afterPatAssrt = afterPatDisc.withPos(pstate.bitPos, pstate.charPos, pstate.inStream.reader)

-      patAssert.map(_.parser).foreach(d => {

-        afterPatAssrt = d.parse1(afterPatAssrt, context)

-        // Pattern fails at the start of the Element

-        if (afterPatAssrt.status != Success) { return afterPatAssrt }

-      })

-

-      // backup again. If all pattern discriminators and/or asserts

-      // have passed, now we parse the element. But we backup

-      // as if the pattern matching had not advanced the state.

-      val beforeEState = afterPatAssrt.withPos(pstate.bitPos, pstate.charPos, pstate.inStream.reader)

-      val postEState = eParser.parse1(beforeEState, context)

-

-      var someSetVarFailed: Option[PState] = None

-

-      var afterSetVar = postEState

-      if (postEState.status == Success) {

-        setVar.map(_.parser).foreach(d => {

-          val afterOneSetVar = d.parse1(afterSetVar, context)

-          if (afterOneSetVar.status == Success) {

-            afterSetVar = afterOneSetVar

-          } else {

-            // a setVariable statement failed. But we want to continue to try 

-            // more of the setVariable statements, as they may be necessary

-            // to evaluate the test discriminator below, and some might 

-            // be successful even if one fails, allowing the discriminator to be true.

-            //

-            // So it's a bit odd, but we're going to just keep parsing using this

-            // failed state as the input to the next setVariable parse step.

-            someSetVarFailed = Some(afterOneSetVar)

-            afterSetVar = afterOneSetVar

-          }

-        })

-      }

-

-      var afterTestDisc = afterSetVar

-      testDiscrim.map(_.parser).foreach(d => {

-        afterTestDisc = d.parse1(afterTestDisc, context)

-        // Tests fail at the end of the Element

-        if (afterTestDisc.status != Success) { return afterTestDisc }

-      })

-

-      // 

-      // We're done with the discriminator, so now we revisit the set variable statements.

-      // If a failure occurred there, then now we can fail out right here.

-      // 

-      someSetVarFailed.exists { return _ }

-

-      // Element evaluation failed, return

-      if (postEState.status != Success) { return postEState }

-

-      var afterTestAssrt = afterTestDisc

-      testAssert.map(_.parser).foreach(d => {

-        afterTestAssrt = d.parse1(afterTestAssrt, context)

-        // Tests fail at the end of the Element

-        if (afterTestAssrt.status != Success) { return afterTestAssrt }

-      })

-

-      afterTestAssrt

-    }

-  }

-

-  def unparser: Unparser = new Unparser(context) {

-    def unparse(start: UState): UState = {

-      // FIXME: setVariables have to execute. We don't do asserts and discriminators when unparsing however.

-      val eUnParser = eGram.unparser

-      val postEState = eUnParser.unparse(start)

-      postEState

-    }

-  }

-

-}

+ */
+
+import edu.illinois.ncsa.daffodil.dsom.DFDLAssert
+import edu.illinois.ncsa.daffodil.dsom.DFDLDiscriminator
+import edu.illinois.ncsa.daffodil.dsom.DFDLSetVariable
+import edu.illinois.ncsa.daffodil.dsom.ElementBase
+import edu.illinois.ncsa.daffodil.grammar.Gram
+import edu.illinois.ncsa.daffodil.grammar.NamedGram
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.TestKind
+import edu.illinois.ncsa.daffodil.exceptions.Assert
+import edu.illinois.ncsa.daffodil.dsom.DiagnosticUtils._
+
+object StmtEval {
+  def apply(context: ElementBase, eGram: Gram) = {
+    // little optimization here. If there are no statements (most common case), then let's 
+    // shortcut and just use the guts parser.
+    val hasStatements = context.notNewVariableInstanceStatements.size > 0
+    if (hasStatements)
+      new StmtEval(context, eGram)
+    else
+      eGram
+  }
+}
+
+class StmtEval(context: ElementBase, eGram: Gram)
+  extends NamedGram(context) {
+
+  // The order of things matters in some cases, so to be consistent we'll always use the 
+  // same order even when it doesn't matter
+
+  // The order of evaluation of statements is:
+  // - pattern discriminators
+  // - pattern asserts
+  // - the parsing of the element itself
+  // - setVariables
+  // - test discriminators (must be attempted even if the parsing of element or setVariable statements fail)
+  // - test asserts
+
+  // requiredEvaluations(patDiscrim, patAssert, eGram, setVar, testDiscrim, testAssert)
+  // Note: above not needed as these are ALWAYS evaluated below.
+
+  val patDiscrim = context.discriminatorStatements.filter(_.testKind == TestKind.Pattern).map(_.gram)
+  val patAssert = context.assertStatements.filter(_.testKind == TestKind.Pattern).map(_.gram)
+  val setVar = context.setVariableStatements.map(_.gram)
+  val testDiscrim = context.discriminatorStatements.filter(_.testKind == TestKind.Expression).map(_.gram)
+  val testAssert = context.assertStatements.filter(_.testKind == TestKind.Expression).map(_.gram)
+
+  val eParser = eGram.parser
+
+  def parser: Parser = new StatementElementParser(context)
+
+  class StatementElementParser(context: ElementBase) extends PrimParser(this, context) {
+
+    Assert.invariant(context.notNewVariableInstanceStatements.size > 0)
+    Assert.invariant(testDiscrim.size <= 1)
+    Assert.invariant(patDiscrim.size <= 1)
+
+    override def toBriefXML(depthLimit: Int = -1): String = {
+      if (depthLimit == 0) "..." else
+        "<StmtEval>" +
+          patDiscrim.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +
+          patAssert.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +
+          eParser.toBriefXML(depthLimit - 1) +
+          setVar.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +
+          testDiscrim.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +
+          testAssert.map { _.parser.toBriefXML(depthLimit - 1) }.mkString +
+          "</StmtEval>"
+    }
+
+    def parse(pstate: PState): PState = {
+      //Removed checks now done at compilation
+
+      var afterPatDisc = pstate //we're first so don't do .withPos(pstate.bitPos, pstate.charPos)
+      patDiscrim.map(_.parser).foreach(d => {
+        afterPatDisc = d.parse1(afterPatDisc, context)
+        // Pattern fails at the start of the Element
+        if (afterPatDisc.status != Success) { return afterPatDisc }
+      })
+
+      // now here we backup and run the pattern Asserts 
+      // against the data at the start of the element's representation again.
+      var afterPatAssrt = afterPatDisc.withPos(pstate.bitPos, pstate.charPos, pstate.inStream.reader)
+      patAssert.map(_.parser).foreach(d => {
+        afterPatAssrt = d.parse1(afterPatAssrt, context)
+        // Pattern fails at the start of the Element
+        if (afterPatAssrt.status != Success) { return afterPatAssrt }
+      })
+
+      // backup again. If all pattern discriminators and/or asserts
+      // have passed, now we parse the element. But we backup
+      // as if the pattern matching had not advanced the state.
+      val beforeEState = afterPatAssrt.withPos(pstate.bitPos, pstate.charPos, pstate.inStream.reader)
+      val postEState = eParser.parse1(beforeEState, context)
+
+      var someSetVarFailed: Option[PState] = None
+
+      var afterSetVar = postEState
+      if (postEState.status == Success) {
+        setVar.map(_.parser).foreach(d => {
+          val afterOneSetVar = d.parse1(afterSetVar, context)
+          if (afterOneSetVar.status == Success) {
+            afterSetVar = afterOneSetVar
+          } else {
+            // a setVariable statement failed. But we want to continue to try 
+            // more of the setVariable statements, as they may be necessary
+            // to evaluate the test discriminator below, and some might 
+            // be successful even if one fails, allowing the discriminator to be true.
+            //
+            // So it's a bit odd, but we're going to just keep parsing using this
+            // failed state as the input to the next setVariable parse step.
+            someSetVarFailed = Some(afterOneSetVar)
+            afterSetVar = afterOneSetVar
+          }
+        })
+      }
+
+      var afterTestDisc = afterSetVar
+      testDiscrim.map(_.parser).foreach(d => {
+        afterTestDisc = d.parse1(afterTestDisc, context)
+        // Tests fail at the end of the Element
+        if (afterTestDisc.status != Success) { return afterTestDisc }
+      })
+
+      // 
+      // We're done with the discriminator, so now we revisit the set variable statements.
+      // If a failure occurred there, then now we can fail out right here.
+      // 
+      someSetVarFailed.exists { return _ }
+
+      // Element evaluation failed, return
+      if (postEState.status != Success) { return postEState }
+
+      var afterTestAssrt = afterTestDisc
+      testAssert.map(_.parser).foreach(d => {
+        afterTestAssrt = d.parse1(afterTestAssrt, context)
+        // Tests fail at the end of the Element
+        if (afterTestAssrt.status != Success) { return afterTestAssrt }
+      })
+
+      afterTestAssrt
+    }
+  }
+
+  def unparser: Unparser = new Unparser(context) {
+    def unparse(start: UState): UState = {
+      // FIXME: setVariables have to execute. We don't do asserts and discriminators when unparsing however.
+      val eUnParser = eGram.unparser
+      val postEState = eUnParser.unparse(start)
+      postEState
+    }
+  }
+
+}
diff --git a/daffodil-lib-unittest/src/test/scala/edu/illinois/ncsa/daffodil/util/TestXSDValidation.scala b/daffodil-lib-unittest/src/test/scala/edu/illinois/ncsa/daffodil/util/TestXSDValidation.scala
index 243d47a..0841b7e 100644
--- a/daffodil-lib-unittest/src/test/scala/edu/illinois/ncsa/daffodil/util/TestXSDValidation.scala
+++ b/daffodil-lib-unittest/src/test/scala/edu/illinois/ncsa/daffodil/util/TestXSDValidation.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.util

+package edu.illinois.ncsa.daffodil.util
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,218 +32,218 @@
  * SOFTWARE.
  */
 
-

-import scala.xml._;

-import junit.framework.Assert._;

-import org.junit.Test;

-import java.io.StringReader;

-import java.io.InputStreamReader; //class TestXSDValidation {

-//

-//  val xmlnsURI = "http://www.w3.org/2001/XMLSchema";

-//  val xsdSubsetURI = "http://www.w3.org/2001/XMLSchema";

-//  val dfdlURI = "http://www.ogf.org/dfdl/dfdl-1.0/";

-//  val xsiURI = "http://www.w3.org/2001/XMLSchema-instance";

-//  val targetNS = "http://example.com";

-//

-//  @Test def testValidationNoTargetNamespace() {

-//    val schema =

-//      <xs:schema xmlns:xs={ xmlnsURI }>

-//        <xs:element name="foo" type="xs:string"/>

-//      </xs:schema>; // xml literals confuse Eclipse's scala plug in. Put them in to make it happy.

-//    val document =

-//      <foo>bar</foo>;

-//    Validator.validateXMLNodes(schema, document)

-//  };

-//  @Test def testValidationWithTargetNamespace() {

-//    val schema =

-//      <xs:schema xmlns:xs={ xmlnsURI } targetNamespace={ targetNS }>

-//        <xs:element name="foo" type="xs:string"/>

-//      </xs:schema>; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.

-//    val document =

-//      <foo xmlns={ targetNS }>bar</foo>;

-//    Validator.validateXMLNodes(schema, document)

-//  };

-//  @Test def testSchemaValidationQualifiedXSPrefixes() {

-//    ; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.

-//    val document = <xs:schema xmlns:xs={ xmlnsURI } targetNamespace={ targetNS }>

-//                     <xs:element name="foo" type="xs:string"/>

-//                   </xs:schema>;

-//    val documentReader = new StringReader(document.toString());

-//    val schemaResource = Misc.getRequiredResource("/xsd/XMLSchema.xsd");

-//    assertTrue(schemaResource != null);

-//    Validator.validateXMLStream(schemaResource.toURI(), documentReader)

-//  };

-//  @Test def testSchemaValidationNoQualifiedXSPrefixes() {

-//    ; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.

-//    val document =

-//      <schema xmlns={ xmlnsURI } xmlns:xs={ xmlnsURI } targetNamespace={ targetNS }>

-//        <element name="foo" type="xs:string"/>

-//      </schema>;

-//    val documentReader = new StringReader(document.toString());

-//    val schemaResource = Misc.getRequiredResource("/xsd/XMLSchema.xsd").toURI();

-//    Validator.validateXMLStream(schemaResource, documentReader)

-//  };

-//  @Test def testSchemaValidationNoTargetNamespaceAndNoQualifiedXSPrefixes() {

-//    ; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.

-//    val document =

-//      <schema xmlns={ xmlnsURI } xmlns:xs={ xmlnsURI }>

-//        <element name="foo" type="xs:string"/>

-//      </schema>;

-//    val documentReader = new StringReader(document.toString());

-//    val schemaResource = Misc.getRequiredResource("/xsd/XMLSchema.xsd").toURI();

-//    Validator.validateXMLStream(schemaResource, documentReader)

-//  };

-//  @Test def testInvalidSchemaValidationNoTargetNamespaceAndNoQualifiedXSPrefixes() {

-//    ; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.

-//    val document =

-//      <schema xmlns={ xmlnsURI } xmlns:xs={ xmlnsURI }>

-//        <element notValidAttribute="foo" type="xs:string"/>

-//      </schema>;

-//    val documentReader = new StringReader(document.toString());

-//    val schemaResource = Misc.getRequiredResource("/xsd/XMLSchema.xsd").toURI();

-//    val ex = intercept[Exception] {

-//      Validator.validateXMLStream(schemaResource, documentReader)

-//    };

-//    val msg = ex.getMessage();

-//    val hasMsgInfo = msg.contains("notValidAttribute");

-//    assertTrue(hasMsgInfo);

-//  };

-//  /**

-//   * constructs a DFDL schema using our defined namespaces including for the subset of XSD we support in DFDL.

-//   */

-//  def xsdUsingSubset(topLevelAnnotations: Seq[Node], contentElements: Seq[Node]) = {

-//    val sch =

-//      <xs:schema xmlns:xs={ xsdSubsetURI } xmlns:dfdl={ dfdlURI } xmlns:xsi={ xsiURI } targetNamespace={ targetNS }>

-//        <xs:annotation>

-//          <xs:appinfo source={ dfdlURI }>

-//            { topLevelAnnotations }

-//          </xs:appinfo>

-//        </xs:annotation>

-//        { contentElements }

-//      </xs:schema>; // xml literals confuse Eclipse's scala plug in. Put them in to make it happy.

-//

-//    val schTxt = sch.toString();

-//    // we do this to get the namespace prefixes in the argument nodes re-interpreted according to our definitions.

-//    // this is a function of scala's parsing, so things have to be lexically enclosed, which is painful at best. 

-//    // I'd really prefer to be able to do some sort of withMyNSByDefault { <foo:bar baz:quux="value"/> }

-//    // but I don't know how to achieve that.

-//    val sch2 = XML.loadString(schTxt);

-//    sch2

-//  };

-//

-//  /**

-//   * constructs a DFDL schema using our defined namespaces including for the subset of XSD we support in DFDL.

-//   * But this variation does not xs: qualify all the XML Schema elements. Many DFDL schemas will get written

-//   * this way, even though it is a sort of frowned-upon style.

-//   */

-//  def xsdUsingSubsetNoPrefix(topLevelAnnotations: Seq[Node], contentElements: Seq[Node]) = {

-//    val sch =

-//      <schema xmlns={ xsdSubsetURI } xmlns:xs={ xsdSubsetURI } xmlns:dfdl={ dfdlURI } xmlns:xsi={ xsiURI } targetNamespace={ targetNS }>

-//        <annotation>

-//          <appinfo source={ dfdlURI }>

-//            { topLevelAnnotations }

-//          </appinfo>

-//        </annotation>

-//        { contentElements }

-//      </schema>; // xml literals confuse Eclipse's scala plug in. Put them in to make it happy.

-//

-//    val schTxt = sch.toString();

-//    // we do this to get the namespace prefixes in the argument nodes re-interpreted according to our definitions.

-//    // this is a function of scala's parsing, so things have to be lexically enclosed, which is painful at best. 

-//    // I'd really prefer to be able to do some sort of withMyNSByDefault { <foo:bar baz:quux="value"/> }

-//    // but I don't know how to achieve that.

-//    val sch2 = XML.loadString(schTxt);

-//    sch2

-//  }; ;

-//  def validateDFDLSchema(xsSchema: NodeSeq) {

-//    val schemaForDFDLSchema = Misc.getRequiredResource(Validator.dfdlSchemaFileName()).toURI();

-//    val xsReader = new StringReader(xsSchema.toString());

-//    Validator.validateXMLStream(schemaForDFDLSchema, xsReader)

-//  };

-//  @Test def testBasicSchemaProcessing() {

-//    val schema =

-//      xsdUsingSubset(Nil,

-//        <xs:element name="foobar" type="xs:int"/>);

-//    validateDFDLSchema(schema)

-//  };

-//  @Test def testBasicInvalidSchemaProcessing() {

-//    val schema =

-//      xsdUsingSubset(Nil,

-//        <xs:element notValidAttribute="foobar" type="xs:int"/>);

-//    val ex = intercept[Exception] {

-//      validateDFDLSchema(schema)

-//    };

-//    val msg = ex.getMessage();

-//    val hasMsgInfo = msg.contains("notValidAttribute");

-//    assertTrue(hasMsgInfo);

-//  };

-//  @Test def testBasicSchemaProcessingNoPrefix() {

-//    val schema =

-//      xsdUsingSubsetNoPrefix(Nil,

-//        <element name="foobar" type="xs:int"/>);

-//    validateDFDLSchema(schema)

-//  };

-//  @Test def testValidationOfXSDSchemaSubsetErrors() {

-//    val schema1 = xsdUsingSubset(Nil,

-//      <xs:element1 name="foo" type="xs:int"/> // <!-- error. No such element -->

-//      )

-//    val ex = intercept[Exception] {

-//      validateDFDLSchema(schema1)

-//    }

-//    // println(ex)

-//    val msg = ex.getMessage()

-//    val hasElement1InMsg = msg.contains("element1");

-//    assertTrue(hasElement1InMsg);

-//  }

-//

-//  @Test def testValidationOfDFDLLongFormAnnotationPropertyNameErrors() {

-//    val schema2 = xsdUsingSubset(

-//      <dfdl:defineFormat name="foo">

-//        <dfdl:format byteOrder1="bigEndian"/><!-- error: incorrect property name -->

-//      </dfdl:defineFormat>,

-//      Nil)

-//    val ex = intercept[Exception] {

-//      validateDFDLSchema(schema2);

-//    }

-//    // should throw a validation error. 

-//    // println(ex)

-//    val msg = ex.getMessage()

-//    val hasErrorText = msg.contains("byteOrder1")

-//    assertTrue(hasErrorText)

-//  };

-//  @Test def testValidationOfDFDLShortFormPropertyValueError() {

-//    val schema2 = xsdUsingSubset(Nil,

-//      <xs:element name="foo" dfdl:byteOrder="invalidValue" type="xs:int"/>);

-//    val ex = intercept[Exception] {

-//      validateDFDLSchema(schema2);

-//    };

-//    // should throw a validation error. 

-//    // println(ex) ;

-//    val msg = ex.getMessage();

-//    val hasErrorText = msg.contains("invalidValue");

-//    assertTrue(hasErrorText)

-//  }

-//

-//  // Test related to Jira task DFDL-76

-//  // Schema below should error out, because name 'bar' isn't a valid internal reference to the type. It should

-//  // be caught as in the xsd namespace, which won't allow it to match the targetNS.

-//  //  @Test def testSchemaValidationReferentialIntegrityChecking() {

-//  //    //

-//  //    // Something about this style of XML Literal breaks Eclipse's scala compilation

-//  //    // and it needs you to put semi-colons all over the place.

-//  //    //

-//  //    val schema =

-//  //      xsdUsingSubsetNoPrefix(Nil,

-//  //                       <element name="foo" type="bar"/>

-//  //                       <complexType name="bar">

-//  //                         <sequence/>

-//  //                       </complexType>

-//  //      ) ;

-//  //     println("THIS TEST SHOULD PASS, BUT VALIDATION ISNT CHECKING FOR THIS COMMON ERROR, SO THE TEST FAILS");

-//  //     val exc = intercept[Exception] {

-//  //    	 validateDFDLSchema(schema)

-//  //     } ;

-//  //     assertTrue(exc.getMessage().contains("bar"))

-//  //  }

-//

-//} // end class

+
+import scala.xml._;
+import junit.framework.Assert._;
+import org.junit.Test;
+import java.io.StringReader;
+import java.io.InputStreamReader; //class TestXSDValidation {
+//
+//  val xmlnsURI = "http://www.w3.org/2001/XMLSchema";
+//  val xsdSubsetURI = "http://www.w3.org/2001/XMLSchema";
+//  val dfdlURI = "http://www.ogf.org/dfdl/dfdl-1.0/";
+//  val xsiURI = "http://www.w3.org/2001/XMLSchema-instance";
+//  val targetNS = "http://example.com";
+//
+//  @Test def testValidationNoTargetNamespace() {
+//    val schema =
+//      <xs:schema xmlns:xs={ xmlnsURI }>
+//        <xs:element name="foo" type="xs:string"/>
+//      </xs:schema>; // xml literals confuse Eclipse's scala plug in. Put them in to make it happy.
+//    val document =
+//      <foo>bar</foo>;
+//    Validator.validateXMLNodes(schema, document)
+//  };
+//  @Test def testValidationWithTargetNamespace() {
+//    val schema =
+//      <xs:schema xmlns:xs={ xmlnsURI } targetNamespace={ targetNS }>
+//        <xs:element name="foo" type="xs:string"/>
+//      </xs:schema>; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.
+//    val document =
+//      <foo xmlns={ targetNS }>bar</foo>;
+//    Validator.validateXMLNodes(schema, document)
+//  };
+//  @Test def testSchemaValidationQualifiedXSPrefixes() {
+//    ; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.
+//    val document = <xs:schema xmlns:xs={ xmlnsURI } targetNamespace={ targetNS }>
+//                     <xs:element name="foo" type="xs:string"/>
+//                   </xs:schema>;
+//    val documentReader = new StringReader(document.toString());
+//    val schemaResource = Misc.getRequiredResource("/xsd/XMLSchema.xsd");
+//    assertTrue(schemaResource != null);
+//    Validator.validateXMLStream(schemaResource.toURI(), documentReader)
+//  };
+//  @Test def testSchemaValidationNoQualifiedXSPrefixes() {
+//    ; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.
+//    val document =
+//      <schema xmlns={ xmlnsURI } xmlns:xs={ xmlnsURI } targetNamespace={ targetNS }>
+//        <element name="foo" type="xs:string"/>
+//      </schema>;
+//    val documentReader = new StringReader(document.toString());
+//    val schemaResource = Misc.getRequiredResource("/xsd/XMLSchema.xsd").toURI();
+//    Validator.validateXMLStream(schemaResource, documentReader)
+//  };
+//  @Test def testSchemaValidationNoTargetNamespaceAndNoQualifiedXSPrefixes() {
+//    ; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.
+//    val document =
+//      <schema xmlns={ xmlnsURI } xmlns:xs={ xmlnsURI }>
+//        <element name="foo" type="xs:string"/>
+//      </schema>;
+//    val documentReader = new StringReader(document.toString());
+//    val schemaResource = Misc.getRequiredResource("/xsd/XMLSchema.xsd").toURI();
+//    Validator.validateXMLStream(schemaResource, documentReader)
+//  };
+//  @Test def testInvalidSchemaValidationNoTargetNamespaceAndNoQualifiedXSPrefixes() {
+//    ; // xml literals confuse Eclipse's scala plug in. Put semicolons in to make it happy.
+//    val document =
+//      <schema xmlns={ xmlnsURI } xmlns:xs={ xmlnsURI }>
+//        <element notValidAttribute="foo" type="xs:string"/>
+//      </schema>;
+//    val documentReader = new StringReader(document.toString());
+//    val schemaResource = Misc.getRequiredResource("/xsd/XMLSchema.xsd").toURI();
+//    val ex = intercept[Exception] {
+//      Validator.validateXMLStream(schemaResource, documentReader)
+//    };
+//    val msg = ex.getMessage();
+//    val hasMsgInfo = msg.contains("notValidAttribute");
+//    assertTrue(hasMsgInfo);
+//  };
+//  /**
+//   * constructs a DFDL schema using our defined namespaces including for the subset of XSD we support in DFDL.
+//   */
+//  def xsdUsingSubset(topLevelAnnotations: Seq[Node], contentElements: Seq[Node]) = {
+//    val sch =
+//      <xs:schema xmlns:xs={ xsdSubsetURI } xmlns:dfdl={ dfdlURI } xmlns:xsi={ xsiURI } targetNamespace={ targetNS }>
+//        <xs:annotation>
+//          <xs:appinfo source={ dfdlURI }>
+//            { topLevelAnnotations }
+//          </xs:appinfo>
+//        </xs:annotation>
+//        { contentElements }
+//      </xs:schema>; // xml literals confuse Eclipse's scala plug in. Put them in to make it happy.
+//
+//    val schTxt = sch.toString();
+//    // we do this to get the namespace prefixes in the argument nodes re-interpreted according to our definitions.
+//    // this is a function of scala's parsing, so things have to be lexically enclosed, which is painful at best. 
+//    // I'd really prefer to be able to do some sort of withMyNSByDefault { <foo:bar baz:quux="value"/> }
+//    // but I don't know how to achieve that.
+//    val sch2 = XML.loadString(schTxt);
+//    sch2
+//  };
+//
+//  /**
+//   * constructs a DFDL schema using our defined namespaces including for the subset of XSD we support in DFDL.
+//   * But this variation does not xs: qualify all the XML Schema elements. Many DFDL schemas will get written
+//   * this way, even though it is a sort of frowned-upon style.
+//   */
+//  def xsdUsingSubsetNoPrefix(topLevelAnnotations: Seq[Node], contentElements: Seq[Node]) = {
+//    val sch =
+//      <schema xmlns={ xsdSubsetURI } xmlns:xs={ xsdSubsetURI } xmlns:dfdl={ dfdlURI } xmlns:xsi={ xsiURI } targetNamespace={ targetNS }>
+//        <annotation>
+//          <appinfo source={ dfdlURI }>
+//            { topLevelAnnotations }
+//          </appinfo>
+//        </annotation>
+//        { contentElements }
+//      </schema>; // xml literals confuse Eclipse's scala plug in. Put them in to make it happy.
+//
+//    val schTxt = sch.toString();
+//    // we do this to get the namespace prefixes in the argument nodes re-interpreted according to our definitions.
+//    // this is a function of scala's parsing, so things have to be lexically enclosed, which is painful at best. 
+//    // I'd really prefer to be able to do some sort of withMyNSByDefault { <foo:bar baz:quux="value"/> }
+//    // but I don't know how to achieve that.
+//    val sch2 = XML.loadString(schTxt);
+//    sch2
+//  }; ;
+//  def validateDFDLSchema(xsSchema: NodeSeq) {
+//    val schemaForDFDLSchema = Misc.getRequiredResource(Validator.dfdlSchemaFileName()).toURI();
+//    val xsReader = new StringReader(xsSchema.toString());
+//    Validator.validateXMLStream(schemaForDFDLSchema, xsReader)
+//  };
+//  @Test def testBasicSchemaProcessing() {
+//    val schema =
+//      xsdUsingSubset(Nil,
+//        <xs:element name="foobar" type="xs:int"/>);
+//    validateDFDLSchema(schema)
+//  };
+//  @Test def testBasicInvalidSchemaProcessing() {
+//    val schema =
+//      xsdUsingSubset(Nil,
+//        <xs:element notValidAttribute="foobar" type="xs:int"/>);
+//    val ex = intercept[Exception] {
+//      validateDFDLSchema(schema)
+//    };
+//    val msg = ex.getMessage();
+//    val hasMsgInfo = msg.contains("notValidAttribute");
+//    assertTrue(hasMsgInfo);
+//  };
+//  @Test def testBasicSchemaProcessingNoPrefix() {
+//    val schema =
+//      xsdUsingSubsetNoPrefix(Nil,
+//        <element name="foobar" type="xs:int"/>);
+//    validateDFDLSchema(schema)
+//  };
+//  @Test def testValidationOfXSDSchemaSubsetErrors() {
+//    val schema1 = xsdUsingSubset(Nil,
+//      <xs:element1 name="foo" type="xs:int"/> // <!-- error. No such element -->
+//      )
+//    val ex = intercept[Exception] {
+//      validateDFDLSchema(schema1)
+//    }
+//    // println(ex)
+//    val msg = ex.getMessage()
+//    val hasElement1InMsg = msg.contains("element1");
+//    assertTrue(hasElement1InMsg);
+//  }
+//
+//  @Test def testValidationOfDFDLLongFormAnnotationPropertyNameErrors() {
+//    val schema2 = xsdUsingSubset(
+//      <dfdl:defineFormat name="foo">
+//        <dfdl:format byteOrder1="bigEndian"/><!-- error: incorrect property name -->
+//      </dfdl:defineFormat>,
+//      Nil)
+//    val ex = intercept[Exception] {
+//      validateDFDLSchema(schema2);
+//    }
+//    // should throw a validation error. 
+//    // println(ex)
+//    val msg = ex.getMessage()
+//    val hasErrorText = msg.contains("byteOrder1")
+//    assertTrue(hasErrorText)
+//  };
+//  @Test def testValidationOfDFDLShortFormPropertyValueError() {
+//    val schema2 = xsdUsingSubset(Nil,
+//      <xs:element name="foo" dfdl:byteOrder="invalidValue" type="xs:int"/>);
+//    val ex = intercept[Exception] {
+//      validateDFDLSchema(schema2);
+//    };
+//    // should throw a validation error. 
+//    // println(ex) ;
+//    val msg = ex.getMessage();
+//    val hasErrorText = msg.contains("invalidValue");
+//    assertTrue(hasErrorText)
+//  }
+//
+//  // Test related to Jira task DFDL-76
+//  // Schema below should error out, because name 'bar' isn't a valid internal reference to the type. It should
+//  // be caught as in the xsd namespace, which won't allow it to match the targetNS.
+//  //  @Test def testSchemaValidationReferentialIntegrityChecking() {
+//  //    //
+//  //    // Something about this style of XML Literal breaks Eclipse's scala compilation
+//  //    // and it needs you to put semi-colons all over the place.
+//  //    //
+//  //    val schema =
+//  //      xsdUsingSubsetNoPrefix(Nil,
+//  //                       <element name="foo" type="bar"/>
+//  //                       <complexType name="bar">
+//  //                         <sequence/>
+//  //                       </complexType>
+//  //      ) ;
+//  //     println("THIS TEST SHOULD PASS, BUT VALIDATION ISNT CHECKING FOR THIS COMMON ERROR, SO THE TEST FAILS");
+//  //     val exc = intercept[Exception] {
+//  //    	 validateDFDLSchema(schema)
+//  //     } ;
+//  //     assertTrue(exc.getMessage().contains("bar"))
+//  //  }
+//
+//} // end class
diff --git a/daffodil-lib/src/main/scala/edu/illinois/ncsa/daffodil/util/Validator.scala b/daffodil-lib/src/main/scala/edu/illinois/ncsa/daffodil/util/Validator.scala
index 9b413c7..b1f5294 100644
--- a/daffodil-lib/src/main/scala/edu/illinois/ncsa/daffodil/util/Validator.scala
+++ b/daffodil-lib/src/main/scala/edu/illinois/ncsa/daffodil/util/Validator.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.util

-

+package edu.illinois.ncsa.daffodil.util
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,115 +30,115 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-//

-// Copyright (C) 2011, 2012 by Michael J. Beckerle, All rights Reserved.

-// Permission is granted to use this software for any purpose so long as 

-// this copyright is preserved in both the source and binary forms, and

-// in any documentation provided with the software. 

-// 

-

-import javax.xml.transform.stream.StreamSource

-import javax.xml.XMLConstants

-import javax.xml.validation.SchemaFactory

-import javax.xml.parsers.SAXParser

-import javax.xml.parsers.SAXParserFactory

-import javax.xml.validation.Schema

-import javax.xml.validation.ValidatorHandler

-import org.xml.sax.XMLReader

-import scala.xml.parsing.NoBindingFactoryAdapter

-import scala.xml._

-import java.io.Reader

-import java.io.File

-import java.io.FileReader

-import java.io.InputStreamReader

-import java.io.FileInputStream

-import java.io.StringReader

-import java.net.URI

-import org.w3c.dom.ls.LSResourceResolver

-

-// Leaving this in temporarily, but commented out.

-// We may want to use this for extra validation passes in the TDML Runner

-// to do a validation pass on the TDML expected Infoset w.r.t. the model and to 

-// do a validation pass on the actual result w.r.t. the model as an XML document.

-//

-//object Validator extends NoBindingFactoryAdapter {

-//

-//  val xr = parser.getXMLReader()

-//  val sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)

-//

-//  def makeParser(): SAXParser = {

-//    var parser: SAXParser = null

-//    try {

-//      val f = SAXParserFactory.newInstance()

-//      f.setNamespaceAware(true)

-//      f.setFeature("http://xml.org/sax/features/namespace-prefixes", true)

-//      //

-//      // Leaving these lines in, to document that they don't work if enabled.

-//      //

-//      //      f.setFeature("http://xml.org/sax/features/external-general-entities", false)

-//      //      f.setFeature("http://xml.org/sax/features/external-parameter-entities", false)

-//      //      

-//      //      // Issue DFDL-76 in Jira - just adding these two lines does check more stuff, but it seems to 

-//      //      // cause all sorts of havoc with not finding various schemas, etc.

-//      //      // Commented out for now pending more thorough investigation of how to fix this issue.

-//      //

-//      //      f.setFeature("http://apache.org/xml/features/validation/schema", true)

-//      //      f.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true)

-//      //      

-//      parser = f.newSAXParser()

-//    } catch {

-//      case e: Exception =>

-//        Console.err.println("error: Unable to instantiate parser")

-//        throw e

-//    }

-//    return parser

-//  }

-//

-//  def validateXMLFiles(schemaFile: File, documentFile: File): Elem = {

-//    val xml = validateXML(new StreamSource(schemaFile), new InputSource(documentFile.toURI().toASCIIString()))

-//    return xml

-//  }

-//

-//  def validateXMLStream(schemaResource: URI, documentReader: Reader, documentSystemId: String = "") = {

-//    val schemaSource = new StreamSource(schemaResource.toASCIIString())

-//    val document = new InputSource(documentReader)

-//    if (documentSystemId != "") document.setSystemId(documentSystemId)

-//    validateXML(schemaSource, document)

-//  }

-//

-//  def validateXML(schemaSource: StreamSource, documentSource: InputSource) = {

-//    val schema = sf.newSchema(schemaSource)

-//    val parser = makeParser()

-//    val xr = parser.getXMLReader()

-//    val vh = schema.newValidatorHandler()

-//    vh.setContentHandler(this)

-//    xr.setContentHandler(vh)

-//    scopeStack.push(TopScope)

-//    xr.parse(documentSource)

-//    scopeStack.pop

-//    rootElem.asInstanceOf[Elem]

-//  }

-//

-//  /**

-//   * Convenient for unit tests

-//   * @param schemaNode

-//   * @param documentNode

-//   * @return

-//   */

-//  def validateXMLNodes(schemaNode: Node, documentNode: NodeSeq): Elem = {

-//    // serialize the scala document XML node back to a string because

-//    // java library wants to read the document from an InputSource.

-//    val documentSource = new InputSource(new StringReader(documentNode.toString()))

-//    val schemaSource = new StreamSource(new StringReader(schemaNode.toString()))

-//    return validateXML(schemaSource, documentSource)

-//  }

-//

-//  /**

-//   * Retrieve a schema that is part of the daffodil-lib.

-//   */

-//  // Note: for a resource, a path begining with "/" means classPath root relative.

-//  def dfdlSchemaFileName(): String = "/xsd/XMLSchema.xsd"

-//

+ */
+
+//
+// Copyright (C) 2011, 2012 by Michael J. Beckerle, All rights Reserved.
+// Permission is granted to use this software for any purpose so long as 
+// this copyright is preserved in both the source and binary forms, and
+// in any documentation provided with the software. 
+// 
+
+import javax.xml.transform.stream.StreamSource
+import javax.xml.XMLConstants
+import javax.xml.validation.SchemaFactory
+import javax.xml.parsers.SAXParser
+import javax.xml.parsers.SAXParserFactory
+import javax.xml.validation.Schema
+import javax.xml.validation.ValidatorHandler
+import org.xml.sax.XMLReader
+import scala.xml.parsing.NoBindingFactoryAdapter
+import scala.xml._
+import java.io.Reader
+import java.io.File
+import java.io.FileReader
+import java.io.InputStreamReader
+import java.io.FileInputStream
+import java.io.StringReader
+import java.net.URI
+import org.w3c.dom.ls.LSResourceResolver
+
+// Leaving this in temporarily, but commented out.
+// We may want to use this for extra validation passes in the TDML Runner
+// to do a validation pass on the TDML expected Infoset w.r.t. the model and to 
+// do a validation pass on the actual result w.r.t. the model as an XML document.
+//
+//object Validator extends NoBindingFactoryAdapter {
+//
+//  val xr = parser.getXMLReader()
+//  val sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
+//
+//  def makeParser(): SAXParser = {
+//    var parser: SAXParser = null
+//    try {
+//      val f = SAXParserFactory.newInstance()
+//      f.setNamespaceAware(true)
+//      f.setFeature("http://xml.org/sax/features/namespace-prefixes", true)
+//      //
+//      // Leaving these lines in, to document that they don't work if enabled.
+//      //
+//      //      f.setFeature("http://xml.org/sax/features/external-general-entities", false)
+//      //      f.setFeature("http://xml.org/sax/features/external-parameter-entities", false)
+//      //      
+//      //      // Issue DFDL-76 in Jira - just adding these two lines does check more stuff, but it seems to 
+//      //      // cause all sorts of havoc with not finding various schemas, etc.
+//      //      // Commented out for now pending more thorough investigation of how to fix this issue.
+//      //
+//      //      f.setFeature("http://apache.org/xml/features/validation/schema", true)
+//      //      f.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true)
+//      //      
+//      parser = f.newSAXParser()
+//    } catch {
+//      case e: Exception =>
+//        Console.err.println("error: Unable to instantiate parser")
+//        throw e
+//    }
+//    return parser
+//  }
+//
+//  def validateXMLFiles(schemaFile: File, documentFile: File): Elem = {
+//    val xml = validateXML(new StreamSource(schemaFile), new InputSource(documentFile.toURI().toASCIIString()))
+//    return xml
+//  }
+//
+//  def validateXMLStream(schemaResource: URI, documentReader: Reader, documentSystemId: String = "") = {
+//    val schemaSource = new StreamSource(schemaResource.toASCIIString())
+//    val document = new InputSource(documentReader)
+//    if (documentSystemId != "") document.setSystemId(documentSystemId)
+//    validateXML(schemaSource, document)
+//  }
+//
+//  def validateXML(schemaSource: StreamSource, documentSource: InputSource) = {
+//    val schema = sf.newSchema(schemaSource)
+//    val parser = makeParser()
+//    val xr = parser.getXMLReader()
+//    val vh = schema.newValidatorHandler()
+//    vh.setContentHandler(this)
+//    xr.setContentHandler(vh)
+//    scopeStack.push(TopScope)
+//    xr.parse(documentSource)
+//    scopeStack.pop
+//    rootElem.asInstanceOf[Elem]
+//  }
+//
+//  /**
+//   * Convenient for unit tests
+//   * @param schemaNode
+//   * @param documentNode
+//   * @return
+//   */
+//  def validateXMLNodes(schemaNode: Node, documentNode: NodeSeq): Elem = {
+//    // serialize the scala document XML node back to a string because
+//    // java library wants to read the document from an InputSource.
+//    val documentSource = new InputSource(new StringReader(documentNode.toString()))
+//    val schemaSource = new StreamSource(new StringReader(schemaNode.toString()))
+//    return validateXML(schemaSource, documentSource)
+//  }
+//
+//  /**
+//   * Retrieve a schema that is part of the daffodil-lib.
+//   */
+//  // Note: for a resource, a path begining with "/" means classPath root relative.
+//  def dfdlSchemaFileName(): String = "/xsd/XMLSchema.xsd"
+//
 //}
\ No newline at end of file
diff --git a/daffodil-perf/src/test/scala/edu/illinois/ncsa/daffodil/many_elem/TestManyElem.scala b/daffodil-perf/src/test/scala/edu/illinois/ncsa/daffodil/many_elem/TestManyElem.scala
index dbf7160..cef0ae0 100644
--- a/daffodil-perf/src/test/scala/edu/illinois/ncsa/daffodil/many_elem/TestManyElem.scala
+++ b/daffodil-perf/src/test/scala/edu/illinois/ncsa/daffodil/many_elem/TestManyElem.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.many_elem

-

+package edu.illinois.ncsa.daffodil.many_elem
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,27 +30,27 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-import org.junit.Rule

-import edu.illinois.ncsa.daffodil.util.Misc

-import java.io.ByteArrayInputStream

-

-class TestManyElem {

-  val testDir = "/edu/illinois/ncsa/daffodil/many_elem/"

-  val aa = testDir + "many_elem.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-

-  @Test def test_many_elem_test() { runner.runPerfTest("many_elem_test") }

-

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+import org.junit.Rule
+import edu.illinois.ncsa.daffodil.util.Misc
+import java.io.ByteArrayInputStream
+
+class TestManyElem {
+  val testDir = "/edu/illinois/ncsa/daffodil/many_elem/"
+  val aa = testDir + "many_elem.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+
+  @Test def test_many_elem_test() { runner.runPerfTest("many_elem_test") }
+
+}
diff --git a/daffodil-perf/src/test/scala/edu/illinois/ncsa/daffodil/schema_chain/TestSchemaChain.scala b/daffodil-perf/src/test/scala/edu/illinois/ncsa/daffodil/schema_chain/TestSchemaChain.scala
index a2241df..5e6c221 100644
--- a/daffodil-perf/src/test/scala/edu/illinois/ncsa/daffodil/schema_chain/TestSchemaChain.scala
+++ b/daffodil-perf/src/test/scala/edu/illinois/ncsa/daffodil/schema_chain/TestSchemaChain.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.schema_chain

-

+package edu.illinois.ncsa.daffodil.schema_chain
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,27 +30,27 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-import org.junit.Rule

-import edu.illinois.ncsa.daffodil.util.Misc

-import java.io.ByteArrayInputStream

-

-class TestSchemaChain {

-  val testDir = "/edu/illinois/ncsa/daffodil/schema_chain/"

-  val aa = testDir + "schema_chain.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-

-  @Test def test_hundred_no_namespaces() { runner.runPerfTest("chain_100_no_namespaces") }

-

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+import org.junit.Rule
+import edu.illinois.ncsa.daffodil.util.Misc
+import java.io.ByteArrayInputStream
+
+class TestSchemaChain {
+  val testDir = "/edu/illinois/ncsa/daffodil/schema_chain/"
+  val aa = testDir + "schema_chain.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+
+  @Test def test_hundred_no_namespaces() { runner.runPerfTest("chain_100_no_namespaces") }
+
+}
diff --git a/daffodil-test-ibm1/src/test/scala-debug/edu/illinois/ncsa/daffodil/TresysTests2.scala b/daffodil-test-ibm1/src/test/scala-debug/edu/illinois/ncsa/daffodil/TresysTests2.scala
index ea23657..66cee75 100644
--- a/daffodil-test-ibm1/src/test/scala-debug/edu/illinois/ncsa/daffodil/TresysTests2.scala
+++ b/daffodil-test-ibm1/src/test/scala-debug/edu/illinois/ncsa/daffodil/TresysTests2.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil

-

+package edu.illinois.ncsa.daffodil
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,126 +30,126 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.util.Misc

-import scala.xml._

-import org.junit.Test

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import org.junit.Test

-

-class TresysTests2 {

-  val testDir = "/test-suite/tresys-contributed/"

-

-  // This test passes now. Left this here to cut/paste for running other tests.    

-  //  val ai = testDir + "AI.tdml"

-  //  lazy val runnerAI = new DFDLTestSuite(Misc.getRequiredResource(ai))

-  //

-  //  @Test def test_AI000() { runnerAI.runOneTest("AI000") }

-

-  //  lazy val runnerMB = new DFDLTestSuite(Misc.getRequiredResource(testDir + "mixed-binary-text.tdml"))

-  //

-  //  @Test def test_codingErrorAction() { runnerMB.runOneTest("codingErrorAction") }

-

-  //  val ab = testDir + "ABLargeData.tdml.dat"

-  //  lazy val runnerAB = new DFDLTestSuite(Misc.getRequiredResource(ab))

-  // Too slow to use in regression. So slow it's a bug.

-  // @Test def test_AB007() { runnerAB.runOneTest("AB007") }

-

-  val ae = testDir + "AE.tdml"

-  lazy val runnerAE = new DFDLTestSuite(Misc.getRequiredResource(ae))

-  @Test def test_AE000() { runnerAE.runOneTest("AE000") } // needs newVariableInstance 

-

-  // Commented out as we have no plans to make this run yet,

-  // and there is no check for recursion, so they die slowly with stack 

-  // overflow

-  //

-  //  val ao = testDir + "AO.tdml"

-  //  lazy val runnerAO = new DFDLTestSuite(Misc.getRequiredResource(ao))

-  //  @Test def test_AO000() { runnerAO.runOneTest("AO000") } // Needs recursion. Not in DFDL v1.0 spec.

-  //  @Test def test_AO001() { runnerAO.runOneTest("AO001") } // Perhaps as an extension to DFDL.

-  //  @Test def test_AO002() { runnerAO.runOneTest("AO002") }

-  //  @Test def test_AO003() { runnerAO.runOneTest("AO003") }

-  //  @Test def test_AO004() { runnerAO.runOneTest("AO004") }

-

-  val ap = testDir + "AP.tdml"

-  lazy val runnerAP = new DFDLTestSuite(Misc.getRequiredResource(ap))

-  @Test def test_AP000() { runnerAP.runOneTest("AP000") } // lengthKind endOfParent

-

-  val at = testDir + "AT.tdml"

-  lazy val runnerAT = new DFDLTestSuite(Misc.getRequiredResource(at))

-  @Test def test_AT000() { runnerAT.runOneTest("AT000") } // needs newVariableInstance

-

-  val au = testDir + "AU.tdml"

-  lazy val runnerAU = new DFDLTestSuite(Misc.getRequiredResource(au))

-  @Test def test_AU000() { runnerAU.runOneTest("AU000") } // packed and bcd 

-

-  val av0 = testDir + "AV000.tdml"

-  lazy val runnerAV000 = new DFDLTestSuite(Misc.getRequiredResource(av0))

-  @Test def test_AV000() { runnerAV000.runOneTest("AV000") } // needs date

-

-  val av1 = testDir + "AV001.tdml"

-  lazy val runnerAV001 = new DFDLTestSuite(Misc.getRequiredResource(av1))

-  @Test def test_AV001() { runnerAV001.runOneTest("AV001") } // needs date

-

-  val av2 = testDir + "AV002.tdml"

-  lazy val runnerAV002 = new DFDLTestSuite(Misc.getRequiredResource(av2))

-  @Test def test_AV002() { runnerAV002.runOneTest("AV002") } // needs date

-

-  val av3 = testDir + "AV003.tdml"

-  lazy val runnerAV003 = new DFDLTestSuite(Misc.getRequiredResource(av3))

-  @Test def test_AV003() { runnerAV003.runOneTest("AV003") } // needs date

-

-  // AW should just be debugged. Uses escape schemes. The test might be wrong

-  // or the implementation. But the feature is implemented.

-  val aw = testDir + "AW.tdml"

-  lazy val runnerAW = new DFDLTestSuite(Misc.getRequiredResource(aw))

-  @Test def test_AW000() { runnerAW.runOneTest("AW000") } // escape schemes

-  @Test def test_AW001() { runnerAW.runOneTest("AW001") }

-

-  // AY should just be debugged. Uses escape schemes. The test might be wrong

-  // or the implementation. But the feature is implemented.

-  val ay = testDir + "AY.tdml"

-  lazy val runnerAY = new DFDLTestSuite(Misc.getRequiredResource(ay))

-  @Test def test_AY000() { runnerAY.runOneTest("AY000") } // escape schemes

-

-  // AZ should just be debugged. Uses escape schemes. The test might be wrong

-  // or the implementation. But the feature is implemented.

-  val az = testDir + "AZ.tdml"

-  lazy val runnerAZ = new DFDLTestSuite(Misc.getRequiredResource(az))

-  @Test def test_AZ000() { runnerAZ.runOneTest("AZ000") } // escape schemes

-

-  // BA should just be debugged. Uses escape schemes. The test might be wrong

-  // or the implementation. But the feature is implemented.

-  val ba = testDir + "BA.tdml"

-  lazy val runnerBA = new DFDLTestSuite(Misc.getRequiredResource(ba))

-  @Test def test_BA000() { runnerBA.runOneTest("BA000") } // escape schemes and delimiters

-

-  val bb = testDir + "BB.tdml"

-  lazy val runnerBB = new DFDLTestSuite(Misc.getRequiredResource(bb))

-  @Test def test_BB000() { runnerBB.runOneTest("BB000") } // occursCountKind stopValue

-

-  val bc = testDir + "BC.tdml"

-  lazy val runnerBC = new DFDLTestSuite(Misc.getRequiredResource(bc))

-  @Test def test_BC000() { runnerBC.runOneTest("BC000") } // text boolean type

-

-  val bd = testDir + "BD.tdml"

-  lazy val runnerBD = new DFDLTestSuite(Misc.getRequiredResource(bd))

-  @Test def test_BD000() { runnerBD.runOneTest("BD000") } // binary boolean type

-

-  val be = testDir + "BE.tdml"

-  lazy val runnerBE = new DFDLTestSuite(Misc.getRequiredResource(be))

-  @Test def test_BE000() { runnerBE.runOneTest("BE000") } // unordered sequences

-  @Test def test_BE001() { runnerBE.runOneTest("BE001") }

-

-  val bf = testDir + "BF.tdml"

-  lazy val runnerBF1 = new DFDLTestSuite(Misc.getRequiredResource(bf))

-  @Test def test_BF000() { runnerBF1.runOneTest("BF000") } // unordered sequences

-  @Test def test_BF001() { runnerBF1.runOneTest("BF001") }

-

-  val bg = testDir + "BG.tdml"

-  lazy val runnerBG = new DFDLTestSuite(Misc.getRequiredResource(bg))

-  @Test def test_BG000() { runnerBG.runOneTest("BG000") } // needs decimal type

-}

+ */
+
+import junit.framework.Assert._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.util.Misc
+import scala.xml._
+import org.junit.Test
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import org.junit.Test
+
+class TresysTests2 {
+  val testDir = "/test-suite/tresys-contributed/"
+
+  // This test passes now. Left this here to cut/paste for running other tests.    
+  //  val ai = testDir + "AI.tdml"
+  //  lazy val runnerAI = new DFDLTestSuite(Misc.getRequiredResource(ai))
+  //
+  //  @Test def test_AI000() { runnerAI.runOneTest("AI000") }
+
+  //  lazy val runnerMB = new DFDLTestSuite(Misc.getRequiredResource(testDir + "mixed-binary-text.tdml"))
+  //
+  //  @Test def test_codingErrorAction() { runnerMB.runOneTest("codingErrorAction") }
+
+  //  val ab = testDir + "ABLargeData.tdml.dat"
+  //  lazy val runnerAB = new DFDLTestSuite(Misc.getRequiredResource(ab))
+  // Too slow to use in regression. So slow it's a bug.
+  // @Test def test_AB007() { runnerAB.runOneTest("AB007") }
+
+  val ae = testDir + "AE.tdml"
+  lazy val runnerAE = new DFDLTestSuite(Misc.getRequiredResource(ae))
+  @Test def test_AE000() { runnerAE.runOneTest("AE000") } // needs newVariableInstance 
+
+  // Commented out as we have no plans to make this run yet,
+  // and there is no check for recursion, so they die slowly with stack 
+  // overflow
+  //
+  //  val ao = testDir + "AO.tdml"
+  //  lazy val runnerAO = new DFDLTestSuite(Misc.getRequiredResource(ao))
+  //  @Test def test_AO000() { runnerAO.runOneTest("AO000") } // Needs recursion. Not in DFDL v1.0 spec.
+  //  @Test def test_AO001() { runnerAO.runOneTest("AO001") } // Perhaps as an extension to DFDL.
+  //  @Test def test_AO002() { runnerAO.runOneTest("AO002") }
+  //  @Test def test_AO003() { runnerAO.runOneTest("AO003") }
+  //  @Test def test_AO004() { runnerAO.runOneTest("AO004") }
+
+  val ap = testDir + "AP.tdml"
+  lazy val runnerAP = new DFDLTestSuite(Misc.getRequiredResource(ap))
+  @Test def test_AP000() { runnerAP.runOneTest("AP000") } // lengthKind endOfParent
+
+  val at = testDir + "AT.tdml"
+  lazy val runnerAT = new DFDLTestSuite(Misc.getRequiredResource(at))
+  @Test def test_AT000() { runnerAT.runOneTest("AT000") } // needs newVariableInstance
+
+  val au = testDir + "AU.tdml"
+  lazy val runnerAU = new DFDLTestSuite(Misc.getRequiredResource(au))
+  @Test def test_AU000() { runnerAU.runOneTest("AU000") } // packed and bcd 
+
+  val av0 = testDir + "AV000.tdml"
+  lazy val runnerAV000 = new DFDLTestSuite(Misc.getRequiredResource(av0))
+  @Test def test_AV000() { runnerAV000.runOneTest("AV000") } // needs date
+
+  val av1 = testDir + "AV001.tdml"
+  lazy val runnerAV001 = new DFDLTestSuite(Misc.getRequiredResource(av1))
+  @Test def test_AV001() { runnerAV001.runOneTest("AV001") } // needs date
+
+  val av2 = testDir + "AV002.tdml"
+  lazy val runnerAV002 = new DFDLTestSuite(Misc.getRequiredResource(av2))
+  @Test def test_AV002() { runnerAV002.runOneTest("AV002") } // needs date
+
+  val av3 = testDir + "AV003.tdml"
+  lazy val runnerAV003 = new DFDLTestSuite(Misc.getRequiredResource(av3))
+  @Test def test_AV003() { runnerAV003.runOneTest("AV003") } // needs date
+
+  // AW should just be debugged. Uses escape schemes. The test might be wrong
+  // or the implementation. But the feature is implemented.
+  val aw = testDir + "AW.tdml"
+  lazy val runnerAW = new DFDLTestSuite(Misc.getRequiredResource(aw))
+  @Test def test_AW000() { runnerAW.runOneTest("AW000") } // escape schemes
+  @Test def test_AW001() { runnerAW.runOneTest("AW001") }
+
+  // AY should just be debugged. Uses escape schemes. The test might be wrong
+  // or the implementation. But the feature is implemented.
+  val ay = testDir + "AY.tdml"
+  lazy val runnerAY = new DFDLTestSuite(Misc.getRequiredResource(ay))
+  @Test def test_AY000() { runnerAY.runOneTest("AY000") } // escape schemes
+
+  // AZ should just be debugged. Uses escape schemes. The test might be wrong
+  // or the implementation. But the feature is implemented.
+  val az = testDir + "AZ.tdml"
+  lazy val runnerAZ = new DFDLTestSuite(Misc.getRequiredResource(az))
+  @Test def test_AZ000() { runnerAZ.runOneTest("AZ000") } // escape schemes
+
+  // BA should just be debugged. Uses escape schemes. The test might be wrong
+  // or the implementation. But the feature is implemented.
+  val ba = testDir + "BA.tdml"
+  lazy val runnerBA = new DFDLTestSuite(Misc.getRequiredResource(ba))
+  @Test def test_BA000() { runnerBA.runOneTest("BA000") } // escape schemes and delimiters
+
+  val bb = testDir + "BB.tdml"
+  lazy val runnerBB = new DFDLTestSuite(Misc.getRequiredResource(bb))
+  @Test def test_BB000() { runnerBB.runOneTest("BB000") } // occursCountKind stopValue
+
+  val bc = testDir + "BC.tdml"
+  lazy val runnerBC = new DFDLTestSuite(Misc.getRequiredResource(bc))
+  @Test def test_BC000() { runnerBC.runOneTest("BC000") } // text boolean type
+
+  val bd = testDir + "BD.tdml"
+  lazy val runnerBD = new DFDLTestSuite(Misc.getRequiredResource(bd))
+  @Test def test_BD000() { runnerBD.runOneTest("BD000") } // binary boolean type
+
+  val be = testDir + "BE.tdml"
+  lazy val runnerBE = new DFDLTestSuite(Misc.getRequiredResource(be))
+  @Test def test_BE000() { runnerBE.runOneTest("BE000") } // unordered sequences
+  @Test def test_BE001() { runnerBE.runOneTest("BE001") }
+
+  val bf = testDir + "BF.tdml"
+  lazy val runnerBF1 = new DFDLTestSuite(Misc.getRequiredResource(bf))
+  @Test def test_BF000() { runnerBF1.runOneTest("BF000") } // unordered sequences
+  @Test def test_BF001() { runnerBF1.runOneTest("BF001") }
+
+  val bg = testDir + "BG.tdml"
+  lazy val runnerBG = new DFDLTestSuite(Misc.getRequiredResource(bg))
+  @Test def test_BG000() { runnerBG.runOneTest("BG000") } // needs decimal type
+}
diff --git a/daffodil-test-ibm1/src/test/scala-new/edu/illinois/ncsa/daffodil/TresysTests3.scala b/daffodil-test-ibm1/src/test/scala-new/edu/illinois/ncsa/daffodil/TresysTests3.scala
index 4ebdc6b..962873b 100644
--- a/daffodil-test-ibm1/src/test/scala-new/edu/illinois/ncsa/daffodil/TresysTests3.scala
+++ b/daffodil-test-ibm1/src/test/scala-new/edu/illinois/ncsa/daffodil/TresysTests3.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil

-

+package edu.illinois.ncsa.daffodil
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,57 +30,57 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import scala.xml._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import edu.illinois.ncsa.daffodil.util.Misc

-import org.junit.Test

-import org.junit.Test

-

-class TresysTests3 {

-  val testDir = "/test-suite/tresys-contributed/"

-

-  lazy val runnerBF = new DFDLTestSuite(Misc.getRequiredResource(testDir + "bitFlagExpression.tdml"))

-

-  @Test def test_testNone() = { runnerBF.runOneTest("testNone") }

-  @Test def test_testOne() { runnerBF.runOneTest("testOne") }

-  @Test def test_testMany() { runnerBF.runOneTest("testMany") }

-

-  val ab = testDir + "ABLargeData.tdml.dat"

-  lazy val runnerAB = new DFDLTestSuite(Misc.getRequiredResource(ab))

-  // Runs, but it is too slow to use in regression tests

-  @Test def test_AB006() { runnerAB.runOneTest("AB006") }

-

-  val af = testDir + "AF.tdml"

-  lazy val runnerAF = new DFDLTestSuite(Misc.getRequiredResource(af))

-  @Test def test_AF000() { runnerAF.runOneTest("AF000") }

-  @Test def test_AF001() { runnerAF.runOneTest("AF001") }

-  @Test def test_AF002() { runnerAF.runOneTest("AF002") }

-

-  val ag = testDir + "AG.tdml"

-  lazy val runnerAG = new DFDLTestSuite(Misc.getRequiredResource(ag))

-  @Test def test_AG000() { runnerAG.runOneTest("AG000") } // OK

-  @Test def test_AG001() { runnerAG.runOneTest("AG001") } // OK

-  @Test def test_AG002() { runnerAG.runOneTest("AG002") } // OK

-

-  val ah = testDir + "AH.tdml"

-  lazy val runnerAH = new DFDLTestSuite(Misc.getRequiredResource(ah))

-  @Test def test_AH000() { runnerAH.runOneTest("AH000") }

-  @Test def test_AH001() { runnerAH.runOneTest("AH001") }

-  @Test def test_AH002() { runnerAH.runOneTest("AH002") }

-

-  // AM is a MIME style example

-  // Wasn't working for lack of occursCountKind, and 

-  // because the bytes were flipped. It was written assuming that

-  // Hex like A1B2 was interpreted as little endian words. I.e, the first

-  // byte in that would be B2.

-  // That's not how TDML works anyway. A1 is first. So by swizzling the indexes

-  // the tests were asking for. Voila, they work.

-  val am = testDir + "AM.tdml"

-  lazy val runnerAM = new DFDLTestSuite(Misc.getRequiredResource(am))

-  @Test def test_AM000() { runnerAM.runOneTest("AM000") }

-  @Test def test_AM001() { runnerAM.runOneTest("AM001") }

-}

+ */
+
+import junit.framework.Assert._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import scala.xml._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import edu.illinois.ncsa.daffodil.util.Misc
+import org.junit.Test
+import org.junit.Test
+
+class TresysTests3 {
+  val testDir = "/test-suite/tresys-contributed/"
+
+  lazy val runnerBF = new DFDLTestSuite(Misc.getRequiredResource(testDir + "bitFlagExpression.tdml"))
+
+  @Test def test_testNone() = { runnerBF.runOneTest("testNone") }
+  @Test def test_testOne() { runnerBF.runOneTest("testOne") }
+  @Test def test_testMany() { runnerBF.runOneTest("testMany") }
+
+  val ab = testDir + "ABLargeData.tdml.dat"
+  lazy val runnerAB = new DFDLTestSuite(Misc.getRequiredResource(ab))
+  // Runs, but it is too slow to use in regression tests
+  @Test def test_AB006() { runnerAB.runOneTest("AB006") }
+
+  val af = testDir + "AF.tdml"
+  lazy val runnerAF = new DFDLTestSuite(Misc.getRequiredResource(af))
+  @Test def test_AF000() { runnerAF.runOneTest("AF000") }
+  @Test def test_AF001() { runnerAF.runOneTest("AF001") }
+  @Test def test_AF002() { runnerAF.runOneTest("AF002") }
+
+  val ag = testDir + "AG.tdml"
+  lazy val runnerAG = new DFDLTestSuite(Misc.getRequiredResource(ag))
+  @Test def test_AG000() { runnerAG.runOneTest("AG000") } // OK
+  @Test def test_AG001() { runnerAG.runOneTest("AG001") } // OK
+  @Test def test_AG002() { runnerAG.runOneTest("AG002") } // OK
+
+  val ah = testDir + "AH.tdml"
+  lazy val runnerAH = new DFDLTestSuite(Misc.getRequiredResource(ah))
+  @Test def test_AH000() { runnerAH.runOneTest("AH000") }
+  @Test def test_AH001() { runnerAH.runOneTest("AH001") }
+  @Test def test_AH002() { runnerAH.runOneTest("AH002") }
+
+  // AM is a MIME style example
+  // Wasn't working for lack of occursCountKind, and 
+  // because the bytes were flipped. It was written assuming that
+  // Hex like A1B2 was interpreted as little endian words. I.e, the first
+  // byte in that would be B2.
+  // That's not how TDML works anyway. A1 is first. So by swizzling the indexes
+  // the tests were asking for. Voila, they work.
+  val am = testDir + "AM.tdml"
+  lazy val runnerAM = new DFDLTestSuite(Misc.getRequiredResource(am))
+  @Test def test_AM000() { runnerAM.runOneTest("AM000") }
+  @Test def test_AM001() { runnerAM.runOneTest("AM001") }
+}
diff --git a/daffodil-test-ibm1/src/test/scala/edu/illinois/ncsa/daffodil/TresysTests.scala b/daffodil-test-ibm1/src/test/scala/edu/illinois/ncsa/daffodil/TresysTests.scala
index 99ff7ab..0061c52 100644
--- a/daffodil-test-ibm1/src/test/scala/edu/illinois/ncsa/daffodil/TresysTests.scala
+++ b/daffodil-test-ibm1/src/test/scala/edu/illinois/ncsa/daffodil/TresysTests.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil

+package edu.illinois.ncsa.daffodil
 
 /* Copyright (c) 2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,102 +32,102 @@
  * SOFTWARE.
  */
 
-import junit.framework.Assert._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import scala.xml._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import edu.illinois.ncsa.daffodil.util.Misc

-import org.junit.Test

-import org.junit.Test

-

-class TresysTests {

-

-  // Debug Template

-  // @Test def test_name() = Debugger.withDebugger { 

-  // LoggingDefaults.setLoggingLevel(LogLevel.Debug)

-  // runner.runOneTest("test_name") 

-  // }

-

-  val testDir = "/test-suite/tresys-contributed/"

-

-  val delimited = testDir + "dpaext1.tdml"

-  lazy val runnerDelimited = new DFDLTestSuite(Misc.getRequiredResource(delimited))

-

-  @Test def test_length_delimited_12_03_controversial() { runnerDelimited.runOneTest("length_delimited_12_03_controversial") }

-

-  val td = testDir + "multiple-diagnostics.tdml"

-  lazy val runnerMD = new DFDLTestSuite(Misc.getRequiredResource(td))

-  runnerMD.setCheckAllTopLevel(true)

-

-  // AX debugged. Uses escape schemes. 

-  val ax = testDir + "AX.tdml"

-  lazy val runnerAX = new DFDLTestSuite(Misc.getRequiredResource(ax))

-  @Test def test_AX000() = { runnerAX.runOneTest("AX000") } // escape schemes

-

-  @Test def test_multiple_diagnostics1() {

-    runnerMD.runOneTest("twoMissingTypeDefErrors")

-  }

-  @Test def test_multiple_diagnostics2() { runnerMD.runOneTest("manyErrors1") }

-  @Test def test_multiple_diagnostics3() { // LoggingDefaults.setLoggingLevel(LogLevel.Compile)

-    runnerMD.runOneTest("manyErrors2")

-  }

-

-  // not found. Debug later.

-  // @Test def test_duplicateDefineFormatsOneSchema() { runnerMD.runOneTest("duplicateDefineFormatsOneSchema") }

-

-  val nsd = testDir + "nested-separator-delimited.tdml"

-  lazy val runnerNSD = new DFDLTestSuite(Misc.getRequiredResource(nsd))

-

-  @Test def test_nested_separator_delimited_baseline() { runnerNSD.runOneTest("baseline") }

-  @Test def test_nested_separator_delimited_basicNest() { runnerNSD.runOneTest("basicNest") }

-  // Fails infinite loop

-  // @Test def test_nested_separator_delimited_basicNest2() { runnerNSD.runOneTest("basicNest2")}

-

-  // Fails, index out of bounds

-  // @Test def test_nested_separator_delimited_nest1() { runnerNSD.runOneTest("nest1")}

-  // Fails infinite loop

-  // @Test def test_nested_separator_delimited_nest2() { runnerNSD.runOneTest("nest2")}    

-  // Fails infinite loop

-  // @Test def test_nested_separator_delimited_nest3() { runnerNSD.runOneTest("nest3")}

-

-  /* Very big test data files, so each is in its own TDML file */

-

-  //  val ab7 = testDir + "ABLargeData.tdml.dat"

-  //  lazy val runnerAB7 = new DFDLTestSuite(Misc.getRequiredResource(ab7))

-  //  @Test def test_AB007() { runnerAB7.runOneTest("AB007") }

-  //  val ab8 = testDir + "AB008.tdml"

-  //  lazy val runnerAB8 = new DFDLTestSuite(Misc.getRequiredResource(ab8))

-  //  @Test def test_AB008() { runnerAB8.runOneTest("AB008") }

-  //  val ab9 = testDir + "AB009.tdml"

-  //  lazy val runnerAB9 = new DFDLTestSuite(Misc.getRequiredResource(ab9))

-  //  @Test def test_AB009() { runnerAB9.runOneTest("AB009") }

-

-  val st = testDir + "simple-type-bases.tdml"

-  lazy val runnerST = new DFDLTestSuite(Misc.getRequiredResource(st))

-  @Test def test_simpleTypeDerivedFromPrimitiveType() { runnerST.runOneTest("st-prim") }

-  @Test def test_simpleTypeChainedDerivations() { runnerST.runOneTest("st-derived") }

-  @Test def test_simpleTypeOverlapPrimError() { runnerST.runOneTest("st-prim-err1") }

-  @Test def test_simpleTypeOverlapSimpleTypeError() { runnerST.runOneTest("st-st-err1") }

-

-  val rd = testDir + "runtime-diagnostics.tdml"

-  lazy val runnerRD = new DFDLTestSuite(Misc.getRequiredResource(rd),

-    validateTDMLFile = false)

-  runnerRD.setCheckAllTopLevel(true)

-

-  @Test def test_runtime_diagnostics1() { runnerRD.runOneTest("PE1") }

-

-  val sq = testDir + "sequence.tdml"

-  lazy val runnerSQ = new DFDLTestSuite(Misc.getRequiredResource(sq))

-  @Test def test_seq1() { runnerSQ.runOneTest("seq1") }

-

-  lazy val runnerMB = new DFDLTestSuite(Misc.getRequiredResource(testDir + "mixed-binary-text.tdml"))

-

-  @Test def test_t1() { runnerMB.runOneTest("t1") }

-  @Test def test_t2() { runnerMB.runOneTest("t2") }

-  @Test def test_t3() { runnerMB.runOneTest("t3") }

-

-  @Test def test_codingErrorAction() { runnerMB.runOneTest("codingErrorAction") }

-

-  val runnerNG = new DFDLTestSuite(Misc.getRequiredResource(testDir + "nested_group_ref.tdml"))

-  @Test def test_nested_group_refs() { runnerNG.runOneTest("nestedGroupRefs") }

-}

+import junit.framework.Assert._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import scala.xml._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import edu.illinois.ncsa.daffodil.util.Misc
+import org.junit.Test
+import org.junit.Test
+
+class TresysTests {
+
+  // Debug Template
+  // @Test def test_name() = Debugger.withDebugger { 
+  // LoggingDefaults.setLoggingLevel(LogLevel.Debug)
+  // runner.runOneTest("test_name") 
+  // }
+
+  val testDir = "/test-suite/tresys-contributed/"
+
+  val delimited = testDir + "dpaext1.tdml"
+  lazy val runnerDelimited = new DFDLTestSuite(Misc.getRequiredResource(delimited))
+
+  @Test def test_length_delimited_12_03_controversial() { runnerDelimited.runOneTest("length_delimited_12_03_controversial") }
+
+  val td = testDir + "multiple-diagnostics.tdml"
+  lazy val runnerMD = new DFDLTestSuite(Misc.getRequiredResource(td))
+  runnerMD.setCheckAllTopLevel(true)
+
+  // AX debugged. Uses escape schemes. 
+  val ax = testDir + "AX.tdml"
+  lazy val runnerAX = new DFDLTestSuite(Misc.getRequiredResource(ax))
+  @Test def test_AX000() = { runnerAX.runOneTest("AX000") } // escape schemes
+
+  @Test def test_multiple_diagnostics1() {
+    runnerMD.runOneTest("twoMissingTypeDefErrors")
+  }
+  @Test def test_multiple_diagnostics2() { runnerMD.runOneTest("manyErrors1") }
+  @Test def test_multiple_diagnostics3() { // LoggingDefaults.setLoggingLevel(LogLevel.Compile)
+    runnerMD.runOneTest("manyErrors2")
+  }
+
+  // not found. Debug later.
+  // @Test def test_duplicateDefineFormatsOneSchema() { runnerMD.runOneTest("duplicateDefineFormatsOneSchema") }
+
+  val nsd = testDir + "nested-separator-delimited.tdml"
+  lazy val runnerNSD = new DFDLTestSuite(Misc.getRequiredResource(nsd))
+
+  @Test def test_nested_separator_delimited_baseline() { runnerNSD.runOneTest("baseline") }
+  @Test def test_nested_separator_delimited_basicNest() { runnerNSD.runOneTest("basicNest") }
+  // Fails infinite loop
+  // @Test def test_nested_separator_delimited_basicNest2() { runnerNSD.runOneTest("basicNest2")}
+
+  // Fails, index out of bounds
+  // @Test def test_nested_separator_delimited_nest1() { runnerNSD.runOneTest("nest1")}
+  // Fails infinite loop
+  // @Test def test_nested_separator_delimited_nest2() { runnerNSD.runOneTest("nest2")}    
+  // Fails infinite loop
+  // @Test def test_nested_separator_delimited_nest3() { runnerNSD.runOneTest("nest3")}
+
+  /* Very big test data files, so each is in its own TDML file */
+
+  //  val ab7 = testDir + "ABLargeData.tdml.dat"
+  //  lazy val runnerAB7 = new DFDLTestSuite(Misc.getRequiredResource(ab7))
+  //  @Test def test_AB007() { runnerAB7.runOneTest("AB007") }
+  //  val ab8 = testDir + "AB008.tdml"
+  //  lazy val runnerAB8 = new DFDLTestSuite(Misc.getRequiredResource(ab8))
+  //  @Test def test_AB008() { runnerAB8.runOneTest("AB008") }
+  //  val ab9 = testDir + "AB009.tdml"
+  //  lazy val runnerAB9 = new DFDLTestSuite(Misc.getRequiredResource(ab9))
+  //  @Test def test_AB009() { runnerAB9.runOneTest("AB009") }
+
+  val st = testDir + "simple-type-bases.tdml"
+  lazy val runnerST = new DFDLTestSuite(Misc.getRequiredResource(st))
+  @Test def test_simpleTypeDerivedFromPrimitiveType() { runnerST.runOneTest("st-prim") }
+  @Test def test_simpleTypeChainedDerivations() { runnerST.runOneTest("st-derived") }
+  @Test def test_simpleTypeOverlapPrimError() { runnerST.runOneTest("st-prim-err1") }
+  @Test def test_simpleTypeOverlapSimpleTypeError() { runnerST.runOneTest("st-st-err1") }
+
+  val rd = testDir + "runtime-diagnostics.tdml"
+  lazy val runnerRD = new DFDLTestSuite(Misc.getRequiredResource(rd),
+    validateTDMLFile = false)
+  runnerRD.setCheckAllTopLevel(true)
+
+  @Test def test_runtime_diagnostics1() { runnerRD.runOneTest("PE1") }
+
+  val sq = testDir + "sequence.tdml"
+  lazy val runnerSQ = new DFDLTestSuite(Misc.getRequiredResource(sq))
+  @Test def test_seq1() { runnerSQ.runOneTest("seq1") }
+
+  lazy val runnerMB = new DFDLTestSuite(Misc.getRequiredResource(testDir + "mixed-binary-text.tdml"))
+
+  @Test def test_t1() { runnerMB.runOneTest("t1") }
+  @Test def test_t2() { runnerMB.runOneTest("t2") }
+  @Test def test_t3() { runnerMB.runOneTest("t3") }
+
+  @Test def test_codingErrorAction() { runnerMB.runOneTest("codingErrorAction") }
+
+  val runnerNG = new DFDLTestSuite(Misc.getRequiredResource(testDir + "nested_group_ref.tdml"))
+  @Test def test_nested_group_refs() { runnerNG.runOneTest("nestedGroupRefs") }
+}
diff --git a/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindImplicitDebug.scala b/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindImplicitDebug.scala
index 9599eb1..6468537 100644
--- a/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindImplicitDebug.scala
+++ b/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindImplicitDebug.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section12.lengthKind

-

+package edu.illinois.ncsa.daffodil.section12.lengthKind
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,25 +30,25 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-

-class TestLengthKindImplicitDebug {

-  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"

-

-  val aa = testDir + "implicit.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-

-  // I just leave one of these here commented out to use as a template

-  // @Test def test_implicit_with_len() { runner.runOneTest("implicit_with_len") }

-

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+
+class TestLengthKindImplicitDebug {
+  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"
+
+  val aa = testDir + "implicit.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+
+  // I just leave one of these here commented out to use as a template
+  // @Test def test_implicit_with_len() { runner.runOneTest("implicit_with_len") }
+
+}
diff --git a/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindPatternDebug.scala b/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindPatternDebug.scala
index 46a8c00..8ebadb5 100644
--- a/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindPatternDebug.scala
+++ b/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindPatternDebug.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.section12.lengthKind

+package edu.illinois.ncsa.daffodil.section12.lengthKind
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,24 +32,24 @@
  * SOFTWARE.
  */
 
-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-

-class TestLengthKindPatternDebug {

-  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"

-  val aa = testDir + "PatternTests.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-

-  @Test def testLengthKindPatternCompound() { runner.runOneTest("LengthKindPatternCompound") }

-  

-  // Now fail due to scanability check 

-  @Test def test_LengthPatternIllegalBits_02() { runner.runOneTest("LengthPatternIllegalBits_02") }

+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+
+class TestLengthKindPatternDebug {
+  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"
+  val aa = testDir + "PatternTests.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+
+  @Test def testLengthKindPatternCompound() { runner.runOneTest("LengthKindPatternCompound") }
+  
+  // Now fail due to scanability check 
+  @Test def test_LengthPatternIllegalBits_02() { runner.runOneTest("LengthPatternIllegalBits_02") }
 }
diff --git a/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressionsDebug.scala b/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressionsDebug.scala
index 3f2f4c9..9b8f185 100644
--- a/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressionsDebug.scala
+++ b/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressionsDebug.scala
@@ -53,16 +53,16 @@
   @Test def test_expressionRules03() { runner.runOneTest("expressionRules03") }
   @Test def test_expressionRules04() { runner.runOneTest("expressionRules04") }
   
-  val testDir2 = "/edu/illinois/ncsa/daffodil/section23/dfdl_functions/"

-  val aa = testDir2 + "Functions.tdml"

-  lazy val runner2 = new DFDLTestSuite(Misc.getRequiredResource(aa))

+  val testDir2 = "/edu/illinois/ncsa/daffodil/section23/dfdl_functions/"
+  val aa = testDir2 + "Functions.tdml"
+  lazy val runner2 = new DFDLTestSuite(Misc.getRequiredResource(aa))
   
-  @Test def test_testBit_3() { runner2.runOneTest("testBit_3") }

+  @Test def test_testBit_3() { runner2.runOneTest("testBit_3") }
 
-  @Test def test_valueLength_0() { runner2.runOneTest("valueLength_0") }

-  @Test def test_valueLength_1() { runner2.runOneTest("valueLength_1") }

+  @Test def test_valueLength_0() { runner2.runOneTest("valueLength_0") }
+  @Test def test_valueLength_1() { runner2.runOneTest("valueLength_1") }
   
-  @Test def test_contentLength_0() { runner2.runOneTest("contentLength_0") }

-  @Test def test_contentLength_1() { runner2.runOneTest("contentLength_1") }

+  @Test def test_contentLength_0() { runner2.runOneTest("contentLength_0") }
+  @Test def test_contentLength_1() { runner2.runOneTest("contentLength_1") }
 
 }
diff --git a/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section24/regular_expressions/TestRegularExpressionsDebug.scala b/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section24/regular_expressions/TestRegularExpressionsDebug.scala
index 1d416a6..397d6a9 100644
--- a/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section24/regular_expressions/TestRegularExpressionsDebug.scala
+++ b/daffodil-test/src/test/scala-debug/edu/illinois/ncsa/daffodil/section24/regular_expressions/TestRegularExpressionsDebug.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section24.regular_expressions

-

+package edu.illinois.ncsa.daffodil.section24.regular_expressions
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,26 +30,26 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-

-class TestRegularExpressions2 {

-  val testDir = "/edu/illinois/ncsa/daffodil/section24/regular_expressions/"

-  val tdml = testDir + "RegularExpressions.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml))

-

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+
+class TestRegularExpressions2 {
+  val testDir = "/edu/illinois/ncsa/daffodil/section24/regular_expressions/"
+  val tdml = testDir + "RegularExpressions.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml))
+
   // Unsupported Java 7 features (should return Schema Definition Errors)
-  @Test def testRegEx_04() { runner.runOneTest("testRegEx_04") }

-  @Test def testRegEx_05() { runner.runOneTest("testRegEx_05") }

-  @Test def testRegEx_06() { runner.runOneTest("testRegEx_06") }

-  @Test def testRegEx_07() { runner.runOneTest("testRegEx_07") }

-}

+  @Test def testRegEx_04() { runner.runOneTest("testRegEx_04") }
+  @Test def testRegEx_05() { runner.runOneTest("testRegEx_05") }
+  @Test def testRegEx_06() { runner.runOneTest("testRegEx_06") }
+  @Test def testRegEx_07() { runner.runOneTest("testRegEx_07") }
+}
diff --git a/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindExplicitNew.scala b/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindExplicitNew.scala
index ff263a5..40a6ad5 100644
--- a/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindExplicitNew.scala
+++ b/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindExplicitNew.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section12.lengthKind

-

+package edu.illinois.ncsa.daffodil.section12.lengthKind
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,37 +30,37 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger.withDebugger

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-

-class TestLengthKindExplicitNew {

-  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"

-  val aa = testDir + "ExplicitTests.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-

-  // Debug Template

-  // @Test def test_name() = Debugger.withDebugger { 

-  // LoggingDefaults.setLoggingLevel(LogLevel.Debug)

-  // runner.runOneTest("test_name") 

-  // }

-

-  @Test def test_ExplicitLengthBitsNotFixed() = { runner.runOneTest("test_ExplicitLengthBitsNotFixed") }

-  @Test def test_ExplicitLengthBitsFixed() = { runner.runOneTest("test_ExplicitLengthBitsFixed") }

-  @Test def test_ExplicitLengthCharsNotFixed() = { runner.runOneTest("test_ExplicitLengthCharsNotFixed") }

-  @Test def test_ExplicitLengthCharsFixed() = { runner.runOneTest("test_ExplicitLengthCharsFixed") }

-

-  

-  @Test def test_ExplicitLengthBytesFixed50() = { runner.runOneTest("test_ExplicitLengthBytesFixed50") }

-

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger.withDebugger
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+
+class TestLengthKindExplicitNew {
+  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"
+  val aa = testDir + "ExplicitTests.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+
+  // Debug Template
+  // @Test def test_name() = Debugger.withDebugger { 
+  // LoggingDefaults.setLoggingLevel(LogLevel.Debug)
+  // runner.runOneTest("test_name") 
+  // }
+
+  @Test def test_ExplicitLengthBitsNotFixed() = { runner.runOneTest("test_ExplicitLengthBitsNotFixed") }
+  @Test def test_ExplicitLengthBitsFixed() = { runner.runOneTest("test_ExplicitLengthBitsFixed") }
+  @Test def test_ExplicitLengthCharsNotFixed() = { runner.runOneTest("test_ExplicitLengthCharsNotFixed") }
+  @Test def test_ExplicitLengthCharsFixed() = { runner.runOneTest("test_ExplicitLengthCharsFixed") }
+
+  
+  @Test def test_ExplicitLengthBytesFixed50() = { runner.runOneTest("test_ExplicitLengthBytesFixed50") }
+
+}
diff --git a/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindPattern2.scala b/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindPattern2.scala
index c1bdac0..1995e23 100644
--- a/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindPattern2.scala
+++ b/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindPattern2.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.section12.lengthKind

+package edu.illinois.ncsa.daffodil.section12.lengthKind
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,23 +32,23 @@
  * SOFTWARE.
  */
 
-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-

-//class TestLengthKindPattern2 {

-//  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"

-//  val aa = testDir + "PatternTests.tdml"

-//  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-//

-//  // @Test def testLengthKindPatternCompound() { runner.runOneTest("LengthKindPatternCompound") }

-//  // @Test def test_LengthPatternIllegalBits_02() { runner.runOneTest("LengthPatternIllegalBits_02") }

-//

+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+
+//class TestLengthKindPattern2 {
+//  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"
+//  val aa = testDir + "PatternTests.tdml"
+//  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+//
+//  // @Test def testLengthKindPatternCompound() { runner.runOneTest("LengthKindPatternCompound") }
+//  // @Test def test_LengthPatternIllegalBits_02() { runner.runOneTest("LengthPatternIllegalBits_02") }
+//
 //}
\ No newline at end of file
diff --git a/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section13/nillable/TestNillableNew.scala b/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section13/nillable/TestNillableNew.scala
index eb85112..f3ee6c9 100644
--- a/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section13/nillable/TestNillableNew.scala
+++ b/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section13/nillable/TestNillableNew.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section13.nillable

-

+package edu.illinois.ncsa.daffodil.section13.nillable
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,31 +30,31 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger.withDebugger

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-

-class TestNillableNew {

-

-  val testDir = "/edu/illinois/ncsa/daffodil/section13/nillable/"

-  val aa = testDir + "nillable.tdml"

-  lazy val runnerAA = new DFDLTestSuite(Misc.getRequiredResource(aa))

-  //@Test def test_nil8() { runnerAA.runOneTest("nil8") }

-  val ln = testDir + "literal-value-nils.tdml"

-  lazy val runnerLN = new DFDLTestSuite(Misc.getRequiredResource(ln))

-  @Test def test_padded_nils() = { runnerLN.runOneTest("test_padded_nils") }

-

-  val testDir_01 = "/edu/illinois/ncsa/daffodil/section06/entities/"

-  val entity = testDir_01 + "entities_01.tdml"

-

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger.withDebugger
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+
+class TestNillableNew {
+
+  val testDir = "/edu/illinois/ncsa/daffodil/section13/nillable/"
+  val aa = testDir + "nillable.tdml"
+  lazy val runnerAA = new DFDLTestSuite(Misc.getRequiredResource(aa))
+  //@Test def test_nil8() { runnerAA.runOneTest("nil8") }
+  val ln = testDir + "literal-value-nils.tdml"
+  lazy val runnerLN = new DFDLTestSuite(Misc.getRequiredResource(ln))
+  @Test def test_padded_nils() = { runnerLN.runOneTest("test_padded_nils") }
+
+  val testDir_01 = "/edu/illinois/ncsa/daffodil/section06/entities/"
+  val entity = testDir_01 + "entities_01.tdml"
+
+}
diff --git a/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressionsNew.scala b/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressionsNew.scala
index 7e07009..09dd8c4 100644
--- a/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressionsNew.scala
+++ b/daffodil-test/src/test/scala-new/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressionsNew.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section23.dfdl_expressions

-

+package edu.illinois.ncsa.daffodil.section23.dfdl_expressions
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,21 +30,21 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-

-class TestDFDLExpressionsNew {

-  val testDir = "/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/"

-  val tdml = testDir + "expressions.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml), validateTDMLFile = false)

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+
+class TestDFDLExpressionsNew {
+  val testDir = "/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/"
+  val tdml = testDir + "expressions.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml), validateTDMLFile = false)
+}
diff --git a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section05/simple_types/TestSimpleTypes.scala b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section05/simple_types/TestSimpleTypes.scala
index ff21589..acc8dcc 100644
--- a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section05/simple_types/TestSimpleTypes.scala
+++ b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section05/simple_types/TestSimpleTypes.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section05.simple_types

-

+package edu.illinois.ncsa.daffodil.section05.simple_types
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,365 +30,365 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-

-class TestSimpleTypes {

-  val testDir = "/edu/illinois/ncsa/daffodil/section05/simple_types/"

-  val aa = testDir + "SimpleTypes.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-

-  @Test def test_nonNegativeInteger() { runner.runOneTest("NonNegativeInteger") }

-  @Test def test_nonNegativeInteger_Fail() { runner.runOneTest("NonNegativeInteger_Fail") }

-

-  val al = testDir + "AL.tdml"

-  lazy val runnerAL = new DFDLTestSuite(Misc.getRequiredResource(al))

-  @Test def test_AL000() { runnerAL.runOneTest("AL000") }

-

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+
+class TestSimpleTypes {
+  val testDir = "/edu/illinois/ncsa/daffodil/section05/simple_types/"
+  val aa = testDir + "SimpleTypes.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+
+  @Test def test_nonNegativeInteger() { runner.runOneTest("NonNegativeInteger") }
+  @Test def test_nonNegativeInteger_Fail() { runner.runOneTest("NonNegativeInteger_Fail") }
+
+  val al = testDir + "AL.tdml"
+  lazy val runnerAL = new DFDLTestSuite(Misc.getRequiredResource(al))
+  @Test def test_AL000() { runnerAL.runOneTest("AL000") }
+
   //@Test def test_hexBinary_rep() { runner.runOneTest("hexBinary_rep") }
 
-  @Test def test_hexBinary_01() { runner.runOneTest("hexBinary_01") }

-  @Test def test_hexBinary_Delimited_01() { runner.runOneTest("hexBinary_Delimited_01") }

-  @Test def test_hexBinary_Delimited_02() { runner.runOneTest("hexBinary_Delimited_02") }

-  @Test def test_hexBinary_Delimited_03() { runner.runOneTest("hexBinary_Delimited_03") }

-  @Test def test_hexBinary_Implicit_01() { runner.runOneTest("hexBinary_Implicit_01") }

-  @Test def test_hexBinary_Implicit_02() { runner.runOneTest("hexBinary_Implicit_02") }

-  @Test def test_hexBinary_Implicit_03() { runner.runOneTest("hexBinary_Implicit_03") }

-  @Test def test_hexBinary_Implicit_03b() { runner.runOneTest("hexBinary_Implicit_03b") }

-  @Test def test_hexBinary_Implicit_03c() { runner.runOneTest("hexBinary_Implicit_03c") }

-  @Test def test_hexBinary_Implicit_04() { runner.runOneTest("hexBinary_Implicit_04") }

-  @Test def test_hexBinary_func() { runner.runOneTest("hexBinary_func") }

-  @Test def test_hexBinary_func_neg() { runner.runOneTest("hexBinary_func_neg") }

-

-  @Test def test_dateTextNumberRep() { runner.runOneTest("dateTextNumberRep") }

-

-  @Test def test_datePattern02() { runner.runOneTest("datePattern02") }

-  @Test def test_datePattern02b() { runner.runOneTest("datePattern02b") }

-  @Test def test_timePattern01() { runner.runOneTest("timePattern01") }

-  @Test def test_timePattern01b() { runner.runOneTest("timePattern01b") }

-

-  @Test def test_dateCalendarLanguage() { runner.runOneTest("dateCalendarLanguage") }

-  @Test def test_dateCalendarLanguage2() { runner.runOneTest("dateCalendarLanguage2") }

-  @Test def test_dateCalendarLanguage3() { runner.runOneTest("dateCalendarLanguage3") }

-  @Test def test_dateCalendarLanguage4() { runner.runOneTest("dateCalendarLanguage4") }

-

-  @Test def test_dateTimeCalendarDaysInFirstWeek() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek") }

-  @Test def test_dateTimeCalendarDaysInFirstWeek2() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek2") }

-  @Test def test_dateTimeCalendarDaysInFirstWeek3() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek3") }

-  @Test def test_dateTimeCalendarDaysInFirstWeek4() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek4") }

-  @Test def test_dateTimeCalendarDaysInFirstWeek5() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek5") }

-  @Test def test_dateTimeCalendarDaysInFirstWeek6() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek6") }

-

-  @Test def test_dateTimeTrim01() { runner.runOneTest("dateTimeTrim01") }

-  @Test def test_dateTimeTrim02() { runner.runOneTest("dateTimeTrim02") }

-  @Test def test_dateTimeTrim03() { runner.runOneTest("dateTimeTrim03") }

-  @Test def test_dateTimeTrim04() { runner.runOneTest("dateTimeTrim04") }

-

-  @Test def test_dateTimePattern01() { runner.runOneTest("dateTimePattern01") }

-  @Test def test_dateTimePattern02() { runner.runOneTest("dateTimePattern02") }

-  @Test def test_dateTimePattern03() { runner.runOneTest("dateTimePattern03") }

-

-  @Test def test_dateEpochFillIn() { runner.runOneTest("dateEpochFillIn") }

-  @Test def test_dateEpochFillIn2() { runner.runOneTest("dateEpochFillIn2") }

-  @Test def test_dateEpochFillIn3() { runner.runOneTest("dateEpochFillIn3") }

-  @Test def test_datePattern08() { runner.runOneTest("datePattern08") }

-  @Test def test_datePattern08b() { runner.runOneTest("datePattern08b") }

-

-  @Test def test_datePattern03() { runner.runOneTest("datePattern03") }

+  @Test def test_hexBinary_01() { runner.runOneTest("hexBinary_01") }
+  @Test def test_hexBinary_Delimited_01() { runner.runOneTest("hexBinary_Delimited_01") }
+  @Test def test_hexBinary_Delimited_02() { runner.runOneTest("hexBinary_Delimited_02") }
+  @Test def test_hexBinary_Delimited_03() { runner.runOneTest("hexBinary_Delimited_03") }
+  @Test def test_hexBinary_Implicit_01() { runner.runOneTest("hexBinary_Implicit_01") }
+  @Test def test_hexBinary_Implicit_02() { runner.runOneTest("hexBinary_Implicit_02") }
+  @Test def test_hexBinary_Implicit_03() { runner.runOneTest("hexBinary_Implicit_03") }
+  @Test def test_hexBinary_Implicit_03b() { runner.runOneTest("hexBinary_Implicit_03b") }
+  @Test def test_hexBinary_Implicit_03c() { runner.runOneTest("hexBinary_Implicit_03c") }
+  @Test def test_hexBinary_Implicit_04() { runner.runOneTest("hexBinary_Implicit_04") }
+  @Test def test_hexBinary_func() { runner.runOneTest("hexBinary_func") }
+  @Test def test_hexBinary_func_neg() { runner.runOneTest("hexBinary_func_neg") }
+
+  @Test def test_dateTextNumberRep() { runner.runOneTest("dateTextNumberRep") }
+
+  @Test def test_datePattern02() { runner.runOneTest("datePattern02") }
+  @Test def test_datePattern02b() { runner.runOneTest("datePattern02b") }
+  @Test def test_timePattern01() { runner.runOneTest("timePattern01") }
+  @Test def test_timePattern01b() { runner.runOneTest("timePattern01b") }
+
+  @Test def test_dateCalendarLanguage() { runner.runOneTest("dateCalendarLanguage") }
+  @Test def test_dateCalendarLanguage2() { runner.runOneTest("dateCalendarLanguage2") }
+  @Test def test_dateCalendarLanguage3() { runner.runOneTest("dateCalendarLanguage3") }
+  @Test def test_dateCalendarLanguage4() { runner.runOneTest("dateCalendarLanguage4") }
+
+  @Test def test_dateTimeCalendarDaysInFirstWeek() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek") }
+  @Test def test_dateTimeCalendarDaysInFirstWeek2() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek2") }
+  @Test def test_dateTimeCalendarDaysInFirstWeek3() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek3") }
+  @Test def test_dateTimeCalendarDaysInFirstWeek4() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek4") }
+  @Test def test_dateTimeCalendarDaysInFirstWeek5() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek5") }
+  @Test def test_dateTimeCalendarDaysInFirstWeek6() { runner.runOneTest("dateTimeCalendarDaysInFirstWeek6") }
+
+  @Test def test_dateTimeTrim01() { runner.runOneTest("dateTimeTrim01") }
+  @Test def test_dateTimeTrim02() { runner.runOneTest("dateTimeTrim02") }
+  @Test def test_dateTimeTrim03() { runner.runOneTest("dateTimeTrim03") }
+  @Test def test_dateTimeTrim04() { runner.runOneTest("dateTimeTrim04") }
+
+  @Test def test_dateTimePattern01() { runner.runOneTest("dateTimePattern01") }
+  @Test def test_dateTimePattern02() { runner.runOneTest("dateTimePattern02") }
+  @Test def test_dateTimePattern03() { runner.runOneTest("dateTimePattern03") }
+
+  @Test def test_dateEpochFillIn() { runner.runOneTest("dateEpochFillIn") }
+  @Test def test_dateEpochFillIn2() { runner.runOneTest("dateEpochFillIn2") }
+  @Test def test_dateEpochFillIn3() { runner.runOneTest("dateEpochFillIn3") }
+  @Test def test_datePattern08() { runner.runOneTest("datePattern08") }
+  @Test def test_datePattern08b() { runner.runOneTest("datePattern08b") }
+
+  @Test def test_datePattern03() { runner.runOneTest("datePattern03") }
   //  @Test def test_datePattern04() { runner.runOneTest("datePattern04") }
-  @Test def test_datePattern05() { runner.runOneTest("datePattern05") }

-  @Test def test_datePattern06() { runner.runOneTest("datePattern06") }

-  @Test def test_datePattern07() { runner.runOneTest("datePattern07") }

-

-  @Test def test_datePatternChoice() { runner.runOneTest("datePatternChoice") }

-

+  @Test def test_datePattern05() { runner.runOneTest("datePattern05") }
+  @Test def test_datePattern06() { runner.runOneTest("datePattern06") }
+  @Test def test_datePattern07() { runner.runOneTest("datePattern07") }
+
+  @Test def test_datePatternChoice() { runner.runOneTest("datePatternChoice") }
+
   //  @Test def test_dateCalendarCenturyStart() { runner.runOneTest("dateCalendarCenturyStart") }
   //  @Test def test_dateCalendarCenturyStart2() { runner.runOneTest("dateCalendarCenturyStart2") }
-

-  @Test def test_dateCalendarDaysInFirstWeek() { runner.runOneTest("dateCalendarDaysInFirstWeek") }

-  @Test def test_dateCalendarDaysInFirstWeek2() { runner.runOneTest("dateCalendarDaysInFirstWeek2") }

-  @Test def test_dateCalendarDaysInFirstWeek3() { runner.runOneTest("dateCalendarDaysInFirstWeek3") }

-  @Test def test_dateCalendarDaysInFirstWeek4() { runner.runOneTest("dateCalendarDaysInFirstWeek4") }

-  @Test def test_dateCalendarDaysInFirstWeek5() { runner.runOneTest("dateCalendarDaysInFirstWeek5") }

-

-  @Test def test_timeSymbols() { runner.runOneTest("timeSymbols") }

-  @Test def test_timeSymbols2() { runner.runOneTest("timeSymbols2") }

-  @Test def test_epochFillIn() { runner.runOneTest("epochFillIn") }

-  @Test def test_epochFillIn2() { runner.runOneTest("epochFillIn2") }

-  @Test def test_epochFillIn3() { runner.runOneTest("epochFillIn3") }

-

-  @Test def test_timeTrim01() { runner.runOneTest("timeTrim01") }

-  @Test def test_timeTrim02() { runner.runOneTest("timeTrim02") }

-  @Test def test_millisecondAccuracy() { runner.runOneTest("millisecondAccuracy") }

-  @Test def test_millisecondAccuracy2() { runner.runOneTest("millisecondAccuracy2") }

-  @Test def test_millisecondAccuracy3() { runner.runOneTest("millisecondAccuracy3") }

-  @Test def test_millisecondAccuracy4() { runner.runOneTest("millisecondAccuracy4") }

-  @Test def test_millisecondAccuracy5() { runner.runOneTest("millisecondAccuracy5") }

-  @Test def test_millisecondAccuracy6() { runner.runOneTest("millisecondAccuracy6") }

-  @Test def test_millisecondAccuracy7() { runner.runOneTest("millisecondAccuracy7") }

-

-  @Test def test_timeStrictCheckPolicy02() { runner.runOneTest("timeStrictCheckPolicy02") }

-

-  @Test def test_timeFormatting() { runner.runOneTest("timeFormatting") }

-  @Test def test_timeFormatting2() { runner.runOneTest("timeFormatting2") }

-  @Test def test_timeFormatting2c() { runner.runOneTest("timeFormatting2c") }

-  @Test def test_timeFormatting2b() { runner.runOneTest("timeFormatting2b") }

-  @Test def test_timeFormatting3() { runner.runOneTest("timeFormatting3") }

-  @Test def test_timeFormatting4() { runner.runOneTest("timeFormatting4") }

-  @Test def test_timeFormatting5() { runner.runOneTest("timeFormatting5") }

-  @Test def test_timeFormatting6() { runner.runOneTest("timeFormatting6") }

-  @Test def test_timeFormatting7() { runner.runOneTest("timeFormatting7") }

-

-  @Test def test_timeCalendarTimeZone() { runner.runOneTest("timeCalendarTimeZone") }

-  @Test def test_timeCalendarTimeZone2() { runner.runOneTest("timeCalendarTimeZone2") }

-  @Test def test_timeCalendarTimeZone3() { runner.runOneTest("timeCalendarTimeZone3") }

-

-  @Test def test_timeZoneFormats() { runner.runOneTest("timeZoneFormats") }

-  @Test def test_timeZoneFormats2() { runner.runOneTest("timeZoneFormats2") }

-  @Test def test_timeZoneFormats3() { runner.runOneTest("timeZoneFormats3") }

-  @Test def test_timeZoneFormats4() { runner.runOneTest("timeZoneFormats4") }

-  @Test def test_timeZoneFormats5() { runner.runOneTest("timeZoneFormats5") }

-  @Test def test_timeZoneFormats6() { runner.runOneTest("timeZoneFormats6") }

-  @Test def test_timeZoneFormats7() { runner.runOneTest("timeZoneFormats7") }

-  @Test def test_timeZoneFormats8() { runner.runOneTest("timeZoneFormats8") }

-  @Test def test_timeZoneFormats9() { runner.runOneTest("timeZoneFormats9") }

-

+
+  @Test def test_dateCalendarDaysInFirstWeek() { runner.runOneTest("dateCalendarDaysInFirstWeek") }
+  @Test def test_dateCalendarDaysInFirstWeek2() { runner.runOneTest("dateCalendarDaysInFirstWeek2") }
+  @Test def test_dateCalendarDaysInFirstWeek3() { runner.runOneTest("dateCalendarDaysInFirstWeek3") }
+  @Test def test_dateCalendarDaysInFirstWeek4() { runner.runOneTest("dateCalendarDaysInFirstWeek4") }
+  @Test def test_dateCalendarDaysInFirstWeek5() { runner.runOneTest("dateCalendarDaysInFirstWeek5") }
+
+  @Test def test_timeSymbols() { runner.runOneTest("timeSymbols") }
+  @Test def test_timeSymbols2() { runner.runOneTest("timeSymbols2") }
+  @Test def test_epochFillIn() { runner.runOneTest("epochFillIn") }
+  @Test def test_epochFillIn2() { runner.runOneTest("epochFillIn2") }
+  @Test def test_epochFillIn3() { runner.runOneTest("epochFillIn3") }
+
+  @Test def test_timeTrim01() { runner.runOneTest("timeTrim01") }
+  @Test def test_timeTrim02() { runner.runOneTest("timeTrim02") }
+  @Test def test_millisecondAccuracy() { runner.runOneTest("millisecondAccuracy") }
+  @Test def test_millisecondAccuracy2() { runner.runOneTest("millisecondAccuracy2") }
+  @Test def test_millisecondAccuracy3() { runner.runOneTest("millisecondAccuracy3") }
+  @Test def test_millisecondAccuracy4() { runner.runOneTest("millisecondAccuracy4") }
+  @Test def test_millisecondAccuracy5() { runner.runOneTest("millisecondAccuracy5") }
+  @Test def test_millisecondAccuracy6() { runner.runOneTest("millisecondAccuracy6") }
+  @Test def test_millisecondAccuracy7() { runner.runOneTest("millisecondAccuracy7") }
+
+  @Test def test_timeStrictCheckPolicy02() { runner.runOneTest("timeStrictCheckPolicy02") }
+
+  @Test def test_timeFormatting() { runner.runOneTest("timeFormatting") }
+  @Test def test_timeFormatting2() { runner.runOneTest("timeFormatting2") }
+  @Test def test_timeFormatting2c() { runner.runOneTest("timeFormatting2c") }
+  @Test def test_timeFormatting2b() { runner.runOneTest("timeFormatting2b") }
+  @Test def test_timeFormatting3() { runner.runOneTest("timeFormatting3") }
+  @Test def test_timeFormatting4() { runner.runOneTest("timeFormatting4") }
+  @Test def test_timeFormatting5() { runner.runOneTest("timeFormatting5") }
+  @Test def test_timeFormatting6() { runner.runOneTest("timeFormatting6") }
+  @Test def test_timeFormatting7() { runner.runOneTest("timeFormatting7") }
+
+  @Test def test_timeCalendarTimeZone() { runner.runOneTest("timeCalendarTimeZone") }
+  @Test def test_timeCalendarTimeZone2() { runner.runOneTest("timeCalendarTimeZone2") }
+  @Test def test_timeCalendarTimeZone3() { runner.runOneTest("timeCalendarTimeZone3") }
+
+  @Test def test_timeZoneFormats() { runner.runOneTest("timeZoneFormats") }
+  @Test def test_timeZoneFormats2() { runner.runOneTest("timeZoneFormats2") }
+  @Test def test_timeZoneFormats3() { runner.runOneTest("timeZoneFormats3") }
+  @Test def test_timeZoneFormats4() { runner.runOneTest("timeZoneFormats4") }
+  @Test def test_timeZoneFormats5() { runner.runOneTest("timeZoneFormats5") }
+  @Test def test_timeZoneFormats6() { runner.runOneTest("timeZoneFormats6") }
+  @Test def test_timeZoneFormats7() { runner.runOneTest("timeZoneFormats7") }
+  @Test def test_timeZoneFormats8() { runner.runOneTest("timeZoneFormats8") }
+  @Test def test_timeZoneFormats9() { runner.runOneTest("timeZoneFormats9") }
+
   //  @Test def test_dateCountDeterminesFormat() { runner.runOneTest("dateCountDeterminesFormat") }
-  @Test def test_dateNonAlphaChars01() { runner.runOneTest("dateNonAlphaChars01") }

-  @Test def test_dateTrim01() { runner.runOneTest("dateTrim01") }

-  @Test def test_dateTrim02() { runner.runOneTest("dateTrim02") }

-  @Test def test_dateTrim03() { runner.runOneTest("dateTrim03") }

-

-  @Test def test_dateCalendarFirstDayOfWeek01() { runner.runOneTest("dateCalendarFirstDayOfWeek01") }

-  @Test def test_dateCalendarFirstDayOfWeek02() { runner.runOneTest("dateCalendarFirstDayOfWeek02") }

-  @Test def test_dateCalendarFirstDayOfWeek03() { runner.runOneTest("dateCalendarFirstDayOfWeek03") }

-  @Test def test_dateCalendarFirstDayOfWeek04() { runner.runOneTest("dateCalendarFirstDayOfWeek04") }

-  @Test def test_timeFractionalSeconds01() { runner.runOneTest("timeFractionalSeconds01") }

-  @Test def test_dateText() { runner.runOneTest("dateText") }

-  @Test def test_timeText() { runner.runOneTest("timeText") }

-  @Test def test_dateTimeText() { runner.runOneTest("dateTimeText") }

-  @Test def test_dateImplicitPattern() { runner.runOneTest("dateImplicitPattern") }

-  @Test def test_dateImplicitPatternFail() { runner.runOneTest("dateImplicitPatternFail") }

+  @Test def test_dateNonAlphaChars01() { runner.runOneTest("dateNonAlphaChars01") }
+  @Test def test_dateTrim01() { runner.runOneTest("dateTrim01") }
+  @Test def test_dateTrim02() { runner.runOneTest("dateTrim02") }
+  @Test def test_dateTrim03() { runner.runOneTest("dateTrim03") }
+
+  @Test def test_dateCalendarFirstDayOfWeek01() { runner.runOneTest("dateCalendarFirstDayOfWeek01") }
+  @Test def test_dateCalendarFirstDayOfWeek02() { runner.runOneTest("dateCalendarFirstDayOfWeek02") }
+  @Test def test_dateCalendarFirstDayOfWeek03() { runner.runOneTest("dateCalendarFirstDayOfWeek03") }
+  @Test def test_dateCalendarFirstDayOfWeek04() { runner.runOneTest("dateCalendarFirstDayOfWeek04") }
+  @Test def test_timeFractionalSeconds01() { runner.runOneTest("timeFractionalSeconds01") }
+  @Test def test_dateText() { runner.runOneTest("dateText") }
+  @Test def test_timeText() { runner.runOneTest("timeText") }
+  @Test def test_dateTimeText() { runner.runOneTest("dateTimeText") }
+  @Test def test_dateImplicitPattern() { runner.runOneTest("dateImplicitPattern") }
+  @Test def test_dateImplicitPatternFail() { runner.runOneTest("dateImplicitPatternFail") }
   @Test def test_timeImplicitPattern() { runner.runOneTest("timeImplicitPattern") }
-  @Test def test_timeImplicitPatternFail() { runner.runOneTest("timeImplicitPatternFail") }

-

-  @Test def test_dateTimeImplicitPattern() { runner.runOneTest("dateTimeImplicitPattern") }

-  @Test def test_dateTimeImplicitPatternFail() { runner.runOneTest("dateTimeImplicitPatternFail") }

-  @Test def test_dateTimeImplicitPattern2() { runner.runOneTest("dateTimeImplicitPatternFail2") }

-  @Test def test_dateTimeImplicitPattern3() { runner.runOneTest("dateTimeImplicitPatternFail3") }

-  @Test def test_dateTimeImplicitPattern4() { runner.runOneTest("dateTimeImplicitPatternFail4") }

-

-  @Test def test_datePattern01() { runner.runOneTest("datePattern01") }

+  @Test def test_timeImplicitPatternFail() { runner.runOneTest("timeImplicitPatternFail") }
+
+  @Test def test_dateTimeImplicitPattern() { runner.runOneTest("dateTimeImplicitPattern") }
+  @Test def test_dateTimeImplicitPatternFail() { runner.runOneTest("dateTimeImplicitPatternFail") }
+  @Test def test_dateTimeImplicitPattern2() { runner.runOneTest("dateTimeImplicitPatternFail2") }
+  @Test def test_dateTimeImplicitPattern3() { runner.runOneTest("dateTimeImplicitPatternFail3") }
+  @Test def test_dateTimeImplicitPattern4() { runner.runOneTest("dateTimeImplicitPatternFail4") }
+
+  @Test def test_datePattern01() { runner.runOneTest("datePattern01") }
   //  @Test def test_datePattern01b() { runner.runOneTest("datePattern01b") }
   @Test def test_timeLaxCheckPolicy01() { runner.runOneTest("timeLaxCheckPolicy01") }
   @Test def test_timeLaxCheckPolicy02() { runner.runOneTest("timeLaxCheckPolicy02") }
   @Test def test_timeLaxCheckPolicy03() { runner.runOneTest("timeLaxCheckPolicy03") }
   @Test def test_dateTimeLaxCheckPolicy01() { runner.runOneTest("dateTimeLaxCheckPolicy01") }
-  @Test def test_dateLaxCheckPolicy01() { runner.runOneTest("dateLaxCheckPolicy01") }

-  @Test def test_dateLaxCheckPolicy02() { runner.runOneTest("dateLaxCheckPolicy02") }

-  @Test def test_dateLaxCheckPolicy03() { runner.runOneTest("dateLaxCheckPolicy03") }

-  @Test def test_dateLaxCheckPolicy04() { runner.runOneTest("dateLaxCheckPolicy04") }

-  @Test def test_dateLaxCheckPolicy05() { runner.runOneTest("dateLaxCheckPolicy05") }

-  @Test def test_dateStrictCheckPolicy01() { runner.runOneTest("dateStrictCheckPolicy01") }

-  @Test def test_timeStrictCheckPolicy01() { runner.runOneTest("timeStrictCheckPolicy01") }

-

-  @Test def test_Long1() { runner.runOneTest("Long1") }

-  @Test def test_BigInteger1() { runner.runOneTest("BigInteger1") }

-  @Test def test_Integer01() { runner.runOneTest("Integer01") }

-  @Test def test_Int01() { runner.runOneTest("Int01") }

-  @Test def test_int_error() { runner.runOneTest("int_error") }

-

+  @Test def test_dateLaxCheckPolicy01() { runner.runOneTest("dateLaxCheckPolicy01") }
+  @Test def test_dateLaxCheckPolicy02() { runner.runOneTest("dateLaxCheckPolicy02") }
+  @Test def test_dateLaxCheckPolicy03() { runner.runOneTest("dateLaxCheckPolicy03") }
+  @Test def test_dateLaxCheckPolicy04() { runner.runOneTest("dateLaxCheckPolicy04") }
+  @Test def test_dateLaxCheckPolicy05() { runner.runOneTest("dateLaxCheckPolicy05") }
+  @Test def test_dateStrictCheckPolicy01() { runner.runOneTest("dateStrictCheckPolicy01") }
+  @Test def test_timeStrictCheckPolicy01() { runner.runOneTest("timeStrictCheckPolicy01") }
+
+  @Test def test_Long1() { runner.runOneTest("Long1") }
+  @Test def test_BigInteger1() { runner.runOneTest("BigInteger1") }
+  @Test def test_Integer01() { runner.runOneTest("Integer01") }
+  @Test def test_Int01() { runner.runOneTest("Int01") }
+  @Test def test_int_error() { runner.runOneTest("int_error") }
+
   // Test warning_exercise moved to scala-debug until warnings are implemented.
-

-  @Test def test_UnsignedNumbers1() { runner.runOneTest("UnsignedNumbers1") }

-  @Test def test_unsignedLong_01() { runner.runOneTest("unsignedLong_01") }

-  @Test def test_unsignedLong_02() { runner.runOneTest("unsignedLong_02") }

-  @Test def test_unsignedLong_03() { runner.runOneTest("unsignedLong_03") }

-  @Test def test_Long2() { runner.runOneTest("Long2") }

-  @Test def test_Long3() { runner.runOneTest("Long3") }

-  @Test def test_Long4() { runner.runOneTest("Long4") }

-  @Test def test_int_error_02() { runner.runOneTest("int_error_02") }

-  @Test def test_int_error_03() { runner.runOneTest("int_error_03") }

-  @Test def test_short_01() { runner.runOneTest("short_01") }

-  @Test def test_short_02() { runner.runOneTest("short_02") }

-  @Test def test_unsignedInt_01() { runner.runOneTest("unsignedInt_01") }

-  @Test def test_unsignedInt_02() { runner.runOneTest("unsignedInt_02") }

-

-  @Test def test_characterDuringValidInt() { runner.runOneTest("characterDuringValidInt") }

-  @Test def test_whiteSpaceAfterLengthExceededInt() { runner.runOneTest("whiteSpaceAfterLengthExceededInt") }

-  @Test def test_whiteSpaceBeforeLengthExceededInt() { runner.runOneTest("whiteSpaceBeforeLengthExceededInt") }

-  @Test def test_whiteSpaceDuringLengthExceededInt() { runner.runOneTest("whiteSpaceDuringLengthExceededInt") }

-  @Test def test_whiteSpaceAfterValidInt() { runner.runOneTest("whiteSpaceAfterValidInt") }

-  @Test def test_characterDuringValidInteger() { runner.runOneTest("characterDuringValidInteger") }

-  @Test def test_whiteSpaceAfterLengthExceededInteger() { runner.runOneTest("whiteSpaceAfterLengthExceededInteger") }

-  @Test def test_whiteSpaceBeforeLengthExceededInteger() { runner.runOneTest("whiteSpaceBeforeLengthExceededInteger") }

-  @Test def test_whiteSpaceDuringLengthExceededInteger() { runner.runOneTest("whiteSpaceDuringLengthExceededInteger") }

-  @Test def test_whiteSpaceBeforeValidInteger() { runner.runOneTest("whiteSpaceBeforeValidInteger") }

+
+  @Test def test_UnsignedNumbers1() { runner.runOneTest("UnsignedNumbers1") }
+  @Test def test_unsignedLong_01() { runner.runOneTest("unsignedLong_01") }
+  @Test def test_unsignedLong_02() { runner.runOneTest("unsignedLong_02") }
+  @Test def test_unsignedLong_03() { runner.runOneTest("unsignedLong_03") }
+  @Test def test_Long2() { runner.runOneTest("Long2") }
+  @Test def test_Long3() { runner.runOneTest("Long3") }
+  @Test def test_Long4() { runner.runOneTest("Long4") }
+  @Test def test_int_error_02() { runner.runOneTest("int_error_02") }
+  @Test def test_int_error_03() { runner.runOneTest("int_error_03") }
+  @Test def test_short_01() { runner.runOneTest("short_01") }
+  @Test def test_short_02() { runner.runOneTest("short_02") }
+  @Test def test_unsignedInt_01() { runner.runOneTest("unsignedInt_01") }
+  @Test def test_unsignedInt_02() { runner.runOneTest("unsignedInt_02") }
+
+  @Test def test_characterDuringValidInt() { runner.runOneTest("characterDuringValidInt") }
+  @Test def test_whiteSpaceAfterLengthExceededInt() { runner.runOneTest("whiteSpaceAfterLengthExceededInt") }
+  @Test def test_whiteSpaceBeforeLengthExceededInt() { runner.runOneTest("whiteSpaceBeforeLengthExceededInt") }
+  @Test def test_whiteSpaceDuringLengthExceededInt() { runner.runOneTest("whiteSpaceDuringLengthExceededInt") }
+  @Test def test_whiteSpaceAfterValidInt() { runner.runOneTest("whiteSpaceAfterValidInt") }
+  @Test def test_characterDuringValidInteger() { runner.runOneTest("characterDuringValidInteger") }
+  @Test def test_whiteSpaceAfterLengthExceededInteger() { runner.runOneTest("whiteSpaceAfterLengthExceededInteger") }
+  @Test def test_whiteSpaceBeforeLengthExceededInteger() { runner.runOneTest("whiteSpaceBeforeLengthExceededInteger") }
+  @Test def test_whiteSpaceDuringLengthExceededInteger() { runner.runOneTest("whiteSpaceDuringLengthExceededInteger") }
+  @Test def test_whiteSpaceBeforeValidInteger() { runner.runOneTest("whiteSpaceBeforeValidInteger") }
   //TODO: Find out why these test generate unexpected errors
-  @Test def test_whiteSpaceDuringValidInteger() { runner.runOneTest("whiteSpaceDuringValidInteger") }

-  @Test def test_whiteSpaceAfterValidInteger() { runner.runOneTest("whiteSpaceAfterValidInteger") }

-  @Test def test_characterDuringValidLong() { runner.runOneTest("characterDuringValidLong") }

-  @Test def test_whiteSpaceAfterLengthExceededLong() { runner.runOneTest("whiteSpaceAfterLengthExceededLong") }

-  @Test def test_whiteSpaceBeforeLengthExceededLong() { runner.runOneTest("whiteSpaceBeforeLengthExceededLong") }

-  @Test def test_whiteSpaceDuringLengthExceededLong() { runner.runOneTest("whiteSpaceDuringLengthExceededLong") }

-  @Test def test_whiteSpaceAfterValidLong() { runner.runOneTest("whiteSpaceAfterValidLong") }

-

-  @Test def test_characterDuringValidShort() { runner.runOneTest("characterDuringValidShort") }

-  @Test def test_whiteSpaceAfterLengthExceededShort() { runner.runOneTest("whiteSpaceAfterLengthExceededShort") }

-  @Test def test_whiteSpaceBeforeLengthExceededShort() { runner.runOneTest("whiteSpaceBeforeLengthExceededShort") }

-  @Test def test_whiteSpaceDuringLengthExceededShort() { runner.runOneTest("whiteSpaceDuringLengthExceededShort") }

-  @Test def test_whiteSpaceAfterValidShort() { runner.runOneTest("whiteSpaceAfterValidShort") }

-  @Test def test_characterDuringValidByte() { runner.runOneTest("characterDuringValidByte") }

-  @Test def test_whiteSpaceAfterLengthExceededByte() { runner.runOneTest("whiteSpaceAfterLengthExceededByte") }

-  @Test def test_whiteSpaceBeforeLengthExceededByte() { runner.runOneTest("whiteSpaceBeforeLengthExceededByte") }

-  @Test def test_whiteSpaceDuringLengthExceededByte() { runner.runOneTest("whiteSpaceDuringLengthExceededByte") }

-  @Test def test_whiteSpaceBeforeValidByte() { runner.runOneTest("whiteSpaceBeforeValidByte") }

+  @Test def test_whiteSpaceDuringValidInteger() { runner.runOneTest("whiteSpaceDuringValidInteger") }
+  @Test def test_whiteSpaceAfterValidInteger() { runner.runOneTest("whiteSpaceAfterValidInteger") }
+  @Test def test_characterDuringValidLong() { runner.runOneTest("characterDuringValidLong") }
+  @Test def test_whiteSpaceAfterLengthExceededLong() { runner.runOneTest("whiteSpaceAfterLengthExceededLong") }
+  @Test def test_whiteSpaceBeforeLengthExceededLong() { runner.runOneTest("whiteSpaceBeforeLengthExceededLong") }
+  @Test def test_whiteSpaceDuringLengthExceededLong() { runner.runOneTest("whiteSpaceDuringLengthExceededLong") }
+  @Test def test_whiteSpaceAfterValidLong() { runner.runOneTest("whiteSpaceAfterValidLong") }
+
+  @Test def test_characterDuringValidShort() { runner.runOneTest("characterDuringValidShort") }
+  @Test def test_whiteSpaceAfterLengthExceededShort() { runner.runOneTest("whiteSpaceAfterLengthExceededShort") }
+  @Test def test_whiteSpaceBeforeLengthExceededShort() { runner.runOneTest("whiteSpaceBeforeLengthExceededShort") }
+  @Test def test_whiteSpaceDuringLengthExceededShort() { runner.runOneTest("whiteSpaceDuringLengthExceededShort") }
+  @Test def test_whiteSpaceAfterValidShort() { runner.runOneTest("whiteSpaceAfterValidShort") }
+  @Test def test_characterDuringValidByte() { runner.runOneTest("characterDuringValidByte") }
+  @Test def test_whiteSpaceAfterLengthExceededByte() { runner.runOneTest("whiteSpaceAfterLengthExceededByte") }
+  @Test def test_whiteSpaceBeforeLengthExceededByte() { runner.runOneTest("whiteSpaceBeforeLengthExceededByte") }
+  @Test def test_whiteSpaceDuringLengthExceededByte() { runner.runOneTest("whiteSpaceDuringLengthExceededByte") }
+  @Test def test_whiteSpaceBeforeValidByte() { runner.runOneTest("whiteSpaceBeforeValidByte") }
   // TODO: Find out why these test generate unexpected errors
-  @Test def test_whiteSpaceDuringValidByte() { runner.runOneTest("whiteSpaceDuringValidByte") }

-  @Test def test_whiteSpaceAfterValidByte() { runner.runOneTest("whiteSpaceAfterValidByte") }

-  @Test def test_characterDuringValidUnsignedInt() { runner.runOneTest("characterDuringValidUnsignedInt") }

-  @Test def test_negativeUnsignedInt() { runner.runOneTest("negativeUnsignedInt") }

-  @Test def test_whiteSpaceAfterLengthExceededUnsignedInt() { runner.runOneTest("whiteSpaceAfterLengthExceededUnsignedInt") }

-  @Test def test_whiteSpaceBeforeLengthExceededUnsignedInt() { runner.runOneTest("whiteSpaceBeforeLengthExceededUnsignedInt") }

-  @Test def test_whiteSpaceDuringLengthExceededUnsignedInt() { runner.runOneTest("whiteSpaceDuringLengthExceededUnsignedInt") }

-  @Test def test_whiteSpaceAfterValidUnsignedInt() { runner.runOneTest("whiteSpaceAfterValidUnsignedInt") }

-

-  @Test def test_characterDuringValidUnsignedByte() { runner.runOneTest("characterDuringValidUnsignedByte") }

-  @Test def test_negativeUnsignedByte() { runner.runOneTest("negativeUnsignedByte") }

-  @Test def test_whiteSpaceAfterLengthExceededUnsignedByte() { runner.runOneTest("whiteSpaceAfterLengthExceededUnsignedByte") }

-  @Test def test_whiteSpaceBeforeLengthExceededUnsignedByte() { runner.runOneTest("whiteSpaceBeforeLengthExceededUnsignedByte") }

-  @Test def test_whiteSpaceDuringLengthExceededUnsignedByte() { runner.runOneTest("whiteSpaceDuringLengthExceededUnsignedByte") }

-  @Test def test_whiteSpaceAfterValidUnsignedByte() { runner.runOneTest("whiteSpaceAfterValidUnsignedByte") }

-  @Test def test_characterDuringValidUnsignedLong() { runner.runOneTest("characterDuringValidUnsignedLong") }

-  @Test def test_negativeUnsignedLong() { runner.runOneTest("negativeUnsignedLong") }

-  @Test def test_whiteSpaceAfterLengthExceededUnsignedLong() { runner.runOneTest("whiteSpaceAfterLengthExceededUnsignedLong") }

-  @Test def test_whiteSpaceBeforeLengthExceededUnsignedLong() { runner.runOneTest("whiteSpaceBeforeLengthExceededUnsignedLong") }

-  @Test def test_whiteSpaceDuringLengthExceededUnsignedLong() { runner.runOneTest("whiteSpaceDuringLengthExceededUnsignedLong") }

-  @Test def test_whiteSpaceBeforeValidUnsignedLong() { runner.runOneTest("whiteSpaceBeforeValidUnsignedLong") }

+  @Test def test_whiteSpaceDuringValidByte() { runner.runOneTest("whiteSpaceDuringValidByte") }
+  @Test def test_whiteSpaceAfterValidByte() { runner.runOneTest("whiteSpaceAfterValidByte") }
+  @Test def test_characterDuringValidUnsignedInt() { runner.runOneTest("characterDuringValidUnsignedInt") }
+  @Test def test_negativeUnsignedInt() { runner.runOneTest("negativeUnsignedInt") }
+  @Test def test_whiteSpaceAfterLengthExceededUnsignedInt() { runner.runOneTest("whiteSpaceAfterLengthExceededUnsignedInt") }
+  @Test def test_whiteSpaceBeforeLengthExceededUnsignedInt() { runner.runOneTest("whiteSpaceBeforeLengthExceededUnsignedInt") }
+  @Test def test_whiteSpaceDuringLengthExceededUnsignedInt() { runner.runOneTest("whiteSpaceDuringLengthExceededUnsignedInt") }
+  @Test def test_whiteSpaceAfterValidUnsignedInt() { runner.runOneTest("whiteSpaceAfterValidUnsignedInt") }
+
+  @Test def test_characterDuringValidUnsignedByte() { runner.runOneTest("characterDuringValidUnsignedByte") }
+  @Test def test_negativeUnsignedByte() { runner.runOneTest("negativeUnsignedByte") }
+  @Test def test_whiteSpaceAfterLengthExceededUnsignedByte() { runner.runOneTest("whiteSpaceAfterLengthExceededUnsignedByte") }
+  @Test def test_whiteSpaceBeforeLengthExceededUnsignedByte() { runner.runOneTest("whiteSpaceBeforeLengthExceededUnsignedByte") }
+  @Test def test_whiteSpaceDuringLengthExceededUnsignedByte() { runner.runOneTest("whiteSpaceDuringLengthExceededUnsignedByte") }
+  @Test def test_whiteSpaceAfterValidUnsignedByte() { runner.runOneTest("whiteSpaceAfterValidUnsignedByte") }
+  @Test def test_characterDuringValidUnsignedLong() { runner.runOneTest("characterDuringValidUnsignedLong") }
+  @Test def test_negativeUnsignedLong() { runner.runOneTest("negativeUnsignedLong") }
+  @Test def test_whiteSpaceAfterLengthExceededUnsignedLong() { runner.runOneTest("whiteSpaceAfterLengthExceededUnsignedLong") }
+  @Test def test_whiteSpaceBeforeLengthExceededUnsignedLong() { runner.runOneTest("whiteSpaceBeforeLengthExceededUnsignedLong") }
+  @Test def test_whiteSpaceDuringLengthExceededUnsignedLong() { runner.runOneTest("whiteSpaceDuringLengthExceededUnsignedLong") }
+  @Test def test_whiteSpaceBeforeValidUnsignedLong() { runner.runOneTest("whiteSpaceBeforeValidUnsignedLong") }
   // TODO: Find out why these test generate unexpected errors
-  @Test def test_whiteSpaceDuringValidUnsignedLong() { runner.runOneTest("whiteSpaceDuringValidUnsignedLong") }

-  @Test def test_whiteSpaceAfterValidUnsignedLong() { runner.runOneTest("whiteSpaceAfterValidUnsignedLong") }

-

-  @Test def test_characterDuringValidUnsignedShort() { runner.runOneTest("characterDuringValidUnsignedShort") }

-  @Test def test_negativeUnsignedShort() { runner.runOneTest("negativeUnsignedShort") }

-  @Test def test_whiteSpaceAfterLengthExceededUnsignedShort() { runner.runOneTest("whiteSpaceAfterLengthExceededUnsignedShort") }

-  @Test def test_whiteSpaceBeforeLengthExceededUnsignedShort() { runner.runOneTest("whiteSpaceBeforeLengthExceededUnsignedShort") }

-  @Test def test_whiteSpaceDuringLengthExceededUnsignedShort() { runner.runOneTest("whiteSpaceDuringLengthExceededUnsignedShort") }

-  @Test def test_whiteSpaceAfterValidUnsignedShort() { runner.runOneTest("whiteSpaceAfterValidUnsignedShort") }

-

+  @Test def test_whiteSpaceDuringValidUnsignedLong() { runner.runOneTest("whiteSpaceDuringValidUnsignedLong") }
+  @Test def test_whiteSpaceAfterValidUnsignedLong() { runner.runOneTest("whiteSpaceAfterValidUnsignedLong") }
+
+  @Test def test_characterDuringValidUnsignedShort() { runner.runOneTest("characterDuringValidUnsignedShort") }
+  @Test def test_negativeUnsignedShort() { runner.runOneTest("negativeUnsignedShort") }
+  @Test def test_whiteSpaceAfterLengthExceededUnsignedShort() { runner.runOneTest("whiteSpaceAfterLengthExceededUnsignedShort") }
+  @Test def test_whiteSpaceBeforeLengthExceededUnsignedShort() { runner.runOneTest("whiteSpaceBeforeLengthExceededUnsignedShort") }
+  @Test def test_whiteSpaceDuringLengthExceededUnsignedShort() { runner.runOneTest("whiteSpaceDuringLengthExceededUnsignedShort") }
+  @Test def test_whiteSpaceAfterValidUnsignedShort() { runner.runOneTest("whiteSpaceAfterValidUnsignedShort") }
+
   //  @Test def test_whiteSpaceAfterLengthExceeded() { runner.runOneTest("whiteSpaceAfterLengthExceeded") }
   //  @Test def test_whiteSpaceBeforeLengthExceeded() { runner.runOneTest("whiteSpaceBeforeLengthExceeded") }
   //  @Test def test_whiteSpaceDuringLengthExceeded() { runner.runOneTest("whiteSpaceDuringLengthExceeded") }
   //  @Test def test_whiteSpaceAfterValidValue() { runner.runOneTest("whiteSpaceAfterValidValue") }
-

-  @Test def test_unsignedShort_01() { runner.runOneTest("unsignedShort_01") }

-  @Test def test_unsignedByte_01() { runner.runOneTest("unsignedByte_01") }

-  @Test def test_unsignedByte_02() { runner.runOneTest("unsignedByte_02") }

-

+
+  @Test def test_unsignedShort_01() { runner.runOneTest("unsignedShort_01") }
+  @Test def test_unsignedByte_01() { runner.runOneTest("unsignedByte_01") }
+  @Test def test_unsignedByte_02() { runner.runOneTest("unsignedByte_02") }
+
   // Test range checking for signed integers too!
-  @Test def test_byte_01() { runner.runOneTest("byte_01") }

-  @Test def test_byte_02() { runner.runOneTest("byte_02") }

-

-  @Test def test_signedShort_binary() { runner.runOneTest("signedShort_binary") }

-  @Test def test_signedShort_binary2() { runner.runOneTest("signedShort_binary2") }

-

-  @Test def test_unsignedShort_binary() { runner.runOneTest("unsignedShort_binary") }

-  @Test def test_unsignedShort_binary2() { runner.runOneTest("unsignedShort_binary2") }

-

-  @Test def test_unsignedLong_binary() { runner.runOneTest("unsignedLong_binary") }

-  @Test def test_unsignedLong_binary2() { runner.runOneTest("unsignedLong_binary2") }

-

-  @Test def test_signedLong_binary() { runner.runOneTest("signedLong_binary") }

-  @Test def test_signedLong_binary2() { runner.runOneTest("signedLong_binary2") }

-

-  @Test def test_unsignedInt_binary() { runner.runOneTest("unsignedInt_binary") }

-  @Test def test_unsignedInt_binary2() { runner.runOneTest("unsignedInt_binary2") }

-

-  val aj = testDir + "AJ.tdml"

-  lazy val runnerAJ = new DFDLTestSuite(Misc.getRequiredResource(aj))

-

-  @Test def test_AJ000() { runnerAJ.runOneTest("AJ000") }

-  @Test def test_AJ001() { runnerAJ.runOneTest("AJ001") }

-

-  val ak = testDir + "AK.tdml"

-  lazy val runnerAK = new DFDLTestSuite(Misc.getRequiredResource(ak))

-  @Test def test_AK000() { runnerAK.runOneTest("AK000") }

-  @Test def test_AK001() { runnerAK.runOneTest("AK001") }

-

-  val testDir_01 = "/edu/illinois/ncsa/daffodil/ibm-tests/"

-  val aa_01 = testDir_01 + "dpaext1.tdml"

-  lazy val runner_01 = new DFDLTestSuite(Misc.getRequiredResource(aa_01))

-  @Test def test_schema_types_5_01() { runner_01.runOneTest("schema_types_5_01") }

-  @Test def test_schema_types_5_02() { runner_01.runOneTest("schema_types_5_02") }

-  @Test def test_schema_types_5_03() { runner_01.runOneTest("schema_types_5_03") }

-  @Test def test_schema_types_5_04() { runner_01.runOneTest("schema_types_5_04") }

-  @Test def test_schema_types_5_05() { runner_01.runOneTest("schema_types_5_05") }

-

-  @Test def test_double_binary_01() { runner.runOneTest("double_binary_01") }

-  @Test def test_double_binary_02() { runner.runOneTest("double_binary_02") }

-  @Test def test_double_binary_03() { runner.runOneTest("double_binary_03") }

-  @Test def test_double_binary_04() { runner.runOneTest("double_binary_04") }

-  @Test def test_double_binary_05() { runner.runOneTest("double_binary_05") }

-

-  @Test def test_byte_binary_01() { runner.runOneTest("byte_binary_01") }

-  @Test def test_byte_binary_02() { runner.runOneTest("byte_binary_02") }

-  @Test def test_byte_binary_03() { runner.runOneTest("byte_binary_03") }

-  @Test def test_byte_binary_04() { runner.runOneTest("byte_binary_04") }

-  @Test def test_byte_binary_05() { runner.runOneTest("byte_binary_05") }

-

-  @Test def test_byte_implicit() { runner.runOneTest("byte_implicit") }

-

-  @Test def test_double_07() { runner.runOneTest("double_07") }

-

-  @Test def test_ubyte_binary_01() { runner.runOneTest("ubyte_binary_01") }

-  @Test def test_ubyte_binary_02() { runner.runOneTest("ubyte_binary_02") }

-  @Test def test_ubyte_binary_03() { runner.runOneTest("ubyte_binary_03") }

-  @Test def test_ubyte_binary_04() { runner.runOneTest("ubyte_binary_04") }

-  @Test def test_ubyte_binary_05() { runner.runOneTest("ubyte_binary_05") }

-

-  @Test def test_ubyte_implicit() { runner.runOneTest("ubyte_implicit") }

-

-  @Test def test_int_binary_01() { runner.runOneTest("int_binary_01") }

-  @Test def test_int_binary_02() { runner.runOneTest("int_binary_02") }

-  @Test def test_int_binary_03() { runner.runOneTest("int_binary_03") }

-  @Test def test_int_binary_04() { runner.runOneTest("int_binary_04") }

-  @Test def test_int_binary_05() { runner.runOneTest("int_binary_05") }

-

-  @Test def test_int_implicit() { runner.runOneTest("int_implicit") }

-

-  @Test def test_integer_binary_01() { runner.runOneTest("integer_binary_01") }

-

+  @Test def test_byte_01() { runner.runOneTest("byte_01") }
+  @Test def test_byte_02() { runner.runOneTest("byte_02") }
+
+  @Test def test_signedShort_binary() { runner.runOneTest("signedShort_binary") }
+  @Test def test_signedShort_binary2() { runner.runOneTest("signedShort_binary2") }
+
+  @Test def test_unsignedShort_binary() { runner.runOneTest("unsignedShort_binary") }
+  @Test def test_unsignedShort_binary2() { runner.runOneTest("unsignedShort_binary2") }
+
+  @Test def test_unsignedLong_binary() { runner.runOneTest("unsignedLong_binary") }
+  @Test def test_unsignedLong_binary2() { runner.runOneTest("unsignedLong_binary2") }
+
+  @Test def test_signedLong_binary() { runner.runOneTest("signedLong_binary") }
+  @Test def test_signedLong_binary2() { runner.runOneTest("signedLong_binary2") }
+
+  @Test def test_unsignedInt_binary() { runner.runOneTest("unsignedInt_binary") }
+  @Test def test_unsignedInt_binary2() { runner.runOneTest("unsignedInt_binary2") }
+
+  val aj = testDir + "AJ.tdml"
+  lazy val runnerAJ = new DFDLTestSuite(Misc.getRequiredResource(aj))
+
+  @Test def test_AJ000() { runnerAJ.runOneTest("AJ000") }
+  @Test def test_AJ001() { runnerAJ.runOneTest("AJ001") }
+
+  val ak = testDir + "AK.tdml"
+  lazy val runnerAK = new DFDLTestSuite(Misc.getRequiredResource(ak))
+  @Test def test_AK000() { runnerAK.runOneTest("AK000") }
+  @Test def test_AK001() { runnerAK.runOneTest("AK001") }
+
+  val testDir_01 = "/edu/illinois/ncsa/daffodil/ibm-tests/"
+  val aa_01 = testDir_01 + "dpaext1.tdml"
+  lazy val runner_01 = new DFDLTestSuite(Misc.getRequiredResource(aa_01))
+  @Test def test_schema_types_5_01() { runner_01.runOneTest("schema_types_5_01") }
+  @Test def test_schema_types_5_02() { runner_01.runOneTest("schema_types_5_02") }
+  @Test def test_schema_types_5_03() { runner_01.runOneTest("schema_types_5_03") }
+  @Test def test_schema_types_5_04() { runner_01.runOneTest("schema_types_5_04") }
+  @Test def test_schema_types_5_05() { runner_01.runOneTest("schema_types_5_05") }
+
+  @Test def test_double_binary_01() { runner.runOneTest("double_binary_01") }
+  @Test def test_double_binary_02() { runner.runOneTest("double_binary_02") }
+  @Test def test_double_binary_03() { runner.runOneTest("double_binary_03") }
+  @Test def test_double_binary_04() { runner.runOneTest("double_binary_04") }
+  @Test def test_double_binary_05() { runner.runOneTest("double_binary_05") }
+
+  @Test def test_byte_binary_01() { runner.runOneTest("byte_binary_01") }
+  @Test def test_byte_binary_02() { runner.runOneTest("byte_binary_02") }
+  @Test def test_byte_binary_03() { runner.runOneTest("byte_binary_03") }
+  @Test def test_byte_binary_04() { runner.runOneTest("byte_binary_04") }
+  @Test def test_byte_binary_05() { runner.runOneTest("byte_binary_05") }
+
+  @Test def test_byte_implicit() { runner.runOneTest("byte_implicit") }
+
+  @Test def test_double_07() { runner.runOneTest("double_07") }
+
+  @Test def test_ubyte_binary_01() { runner.runOneTest("ubyte_binary_01") }
+  @Test def test_ubyte_binary_02() { runner.runOneTest("ubyte_binary_02") }
+  @Test def test_ubyte_binary_03() { runner.runOneTest("ubyte_binary_03") }
+  @Test def test_ubyte_binary_04() { runner.runOneTest("ubyte_binary_04") }
+  @Test def test_ubyte_binary_05() { runner.runOneTest("ubyte_binary_05") }
+
+  @Test def test_ubyte_implicit() { runner.runOneTest("ubyte_implicit") }
+
+  @Test def test_int_binary_01() { runner.runOneTest("int_binary_01") }
+  @Test def test_int_binary_02() { runner.runOneTest("int_binary_02") }
+  @Test def test_int_binary_03() { runner.runOneTest("int_binary_03") }
+  @Test def test_int_binary_04() { runner.runOneTest("int_binary_04") }
+  @Test def test_int_binary_05() { runner.runOneTest("int_binary_05") }
+
+  @Test def test_int_implicit() { runner.runOneTest("int_implicit") }
+
+  @Test def test_integer_binary_01() { runner.runOneTest("integer_binary_01") }
+
   //  @Test def test_posinteger_binary_01() { runner.runOneTest("nonNegInt_binary_01") }
-

-  @Test def test_literalChar_padding() { runner.runOneTest("literalChar_padding") }

-  @Test def test_literalChar_padding2() { runner.runOneTest("literalChar_padding2") }

-  @Test def test_literalChar_padding3() { runner.runOneTest("literalChar_padding3") }

-  @Test def test_literalChar_padding4() { runner.runOneTest("literalChar_padding4") }

-  @Test def test_literalChar_padding5() { runner.runOneTest("literalChar_padding5") }

-  @Test def test_literalChar_padding6() { runner.runOneTest("literalChar_padding6") }

-  @Test def test_charEntity_padding1() { runner.runOneTest("charEntity_padding1") }

-  @Test def test_charEntity_padding2() { runner.runOneTest("charEntity_padding2") }

-  @Test def test_charEntity_padding3() { runner.runOneTest("charEntity_padding3") }

-  @Test def test_number_padding() { runner.runOneTest("number_padding") }

-  @Test def test_number_padding2() { runner.runOneTest("number_padding2") }

-  @Test def test_number_padding3() { runner.runOneTest("number_padding3") }

-  @Test def test_number_padding4() { runner.runOneTest("number_padding4") }

-  @Test def test_number_padding5() { runner.runOneTest("number_padding5") }

-  @Test def test_padding_escape() { runner.runOneTest("padding_escape") }

-  @Test def test_padding_nil() { runner.runOneTest("padding_nil") }

-  @Test def test_padding_nil2() { runner.runOneTest("padding_nil2") }

-  @Test def test_justification_1() { runner.runOneTest("justification_1") }

-  

-  // Verification that user's test works for DFDL-677

-  @Test def test_unsignedInt() { runner.runOneTest("TestUnsignedInt") }

-

+
+  @Test def test_literalChar_padding() { runner.runOneTest("literalChar_padding") }
+  @Test def test_literalChar_padding2() { runner.runOneTest("literalChar_padding2") }
+  @Test def test_literalChar_padding3() { runner.runOneTest("literalChar_padding3") }
+  @Test def test_literalChar_padding4() { runner.runOneTest("literalChar_padding4") }
+  @Test def test_literalChar_padding5() { runner.runOneTest("literalChar_padding5") }
+  @Test def test_literalChar_padding6() { runner.runOneTest("literalChar_padding6") }
+  @Test def test_charEntity_padding1() { runner.runOneTest("charEntity_padding1") }
+  @Test def test_charEntity_padding2() { runner.runOneTest("charEntity_padding2") }
+  @Test def test_charEntity_padding3() { runner.runOneTest("charEntity_padding3") }
+  @Test def test_number_padding() { runner.runOneTest("number_padding") }
+  @Test def test_number_padding2() { runner.runOneTest("number_padding2") }
+  @Test def test_number_padding3() { runner.runOneTest("number_padding3") }
+  @Test def test_number_padding4() { runner.runOneTest("number_padding4") }
+  @Test def test_number_padding5() { runner.runOneTest("number_padding5") }
+  @Test def test_padding_escape() { runner.runOneTest("padding_escape") }
+  @Test def test_padding_nil() { runner.runOneTest("padding_nil") }
+  @Test def test_padding_nil2() { runner.runOneTest("padding_nil2") }
+  @Test def test_justification_1() { runner.runOneTest("justification_1") }
+  
+  // Verification that user's test works for DFDL-677
+  @Test def test_unsignedInt() { runner.runOneTest("TestUnsignedInt") }
+
 }
diff --git a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section08/property_scoping/TestPropertyScoping.scala b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section08/property_scoping/TestPropertyScoping.scala
index 3e6925a..1c74ce3 100644
--- a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section08/property_scoping/TestPropertyScoping.scala
+++ b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section08/property_scoping/TestPropertyScoping.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section08.property_scoping

-

+package edu.illinois.ncsa.daffodil.section08.property_scoping
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,48 +30,48 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-

-class TestPropertyScoping {

-  val testDir = "/edu/illinois/ncsa/daffodil/section08/property_scoping/"

-  val aa = testDir + "PropertyScoping.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-

-  @Test def test_property_scoping_01() { runner.runOneTest("property_scoping_01") }

-  @Test def test_property_scoping_06() { runner.runOneTest("property_scoping_06") }

-  @Test def test_property_group_ref() { runner.runOneTest("group_ref") }

-

-  // Moved back to debug, because validation is no longer detecting the error it is

-  // expecting

-  // @Test def test_property_shortFormSchemaFail() { runner.runOneTest("shortFormSchemaFail") }

-

-  @Test def test_format_nesting_01() { runner.runOneTest("format_nesting_01") }

-

-  val tdml = testDir + "PropertyScoping_01.tdml"

-  lazy val runner_01 = new DFDLTestSuite(Misc.getRequiredResource(tdml))

-

-  @Test def test_property_scoping_02() { runner_01.runOneTest("property_scoping_02") }

-  @Test def test_property_scoping_03() = { runner_01.runOneTest("property_scoping_03") }

-  @Test def test_property_scoping_04() { runner_01.runOneTest("property_scoping_04") }

-  @Test def test_property_scoping_05() { runner_01.runOneTest("property_scoping_05") }

-  @Test def test_property_scoping_07() { runner_01.runOneTest("property_scoping_07") }

-  @Test def test_property_scoping_08() { runner_01.runOneTest("property_scoping_08") }

-  @Test def test_property_scoping_09() { runner_01.runOneTest("property_scoping_09") }

-  @Test def test_property_scoping_10() { runner_01.runOneTest("property_scoping_10") }

-  @Test def test_property_scoping_11() { runner_01.runOneTest("property_scoping_11") }

-  @Test def testNearestEnclosingSequenceElementRef() { runner_01.runOneTest("NearestEnclosingSequenceElementRef") }

-

-  @Test def test_property_refElementFormFail() = { runner_01.runOneTest("refElementFormFail") }

-

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+
+class TestPropertyScoping {
+  val testDir = "/edu/illinois/ncsa/daffodil/section08/property_scoping/"
+  val aa = testDir + "PropertyScoping.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+
+  @Test def test_property_scoping_01() { runner.runOneTest("property_scoping_01") }
+  @Test def test_property_scoping_06() { runner.runOneTest("property_scoping_06") }
+  @Test def test_property_group_ref() { runner.runOneTest("group_ref") }
+
+  // Moved back to debug, because validation is no longer detecting the error it is
+  // expecting
+  // @Test def test_property_shortFormSchemaFail() { runner.runOneTest("shortFormSchemaFail") }
+
+  @Test def test_format_nesting_01() { runner.runOneTest("format_nesting_01") }
+
+  val tdml = testDir + "PropertyScoping_01.tdml"
+  lazy val runner_01 = new DFDLTestSuite(Misc.getRequiredResource(tdml))
+
+  @Test def test_property_scoping_02() { runner_01.runOneTest("property_scoping_02") }
+  @Test def test_property_scoping_03() = { runner_01.runOneTest("property_scoping_03") }
+  @Test def test_property_scoping_04() { runner_01.runOneTest("property_scoping_04") }
+  @Test def test_property_scoping_05() { runner_01.runOneTest("property_scoping_05") }
+  @Test def test_property_scoping_07() { runner_01.runOneTest("property_scoping_07") }
+  @Test def test_property_scoping_08() { runner_01.runOneTest("property_scoping_08") }
+  @Test def test_property_scoping_09() { runner_01.runOneTest("property_scoping_09") }
+  @Test def test_property_scoping_10() { runner_01.runOneTest("property_scoping_10") }
+  @Test def test_property_scoping_11() { runner_01.runOneTest("property_scoping_11") }
+  @Test def testNearestEnclosingSequenceElementRef() { runner_01.runOneTest("NearestEnclosingSequenceElementRef") }
+
+  @Test def test_property_refElementFormFail() = { runner_01.runOneTest("refElementFormFail") }
+
+}
diff --git a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section12/delimiter_properties/TestDelimiterProperties.scala b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section12/delimiter_properties/TestDelimiterProperties.scala
index 742c872..9927372 100644
--- a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section12/delimiter_properties/TestDelimiterProperties.scala
+++ b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section12/delimiter_properties/TestDelimiterProperties.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.section12.delimiter_properties

+package edu.illinois.ncsa.daffodil.section12.delimiter_properties
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,54 +32,54 @@
  * SOFTWARE.
  */
 
-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-

-class TestDelimiterProperties {

-

-  val testDir_01 = "/edu/illinois/ncsa/daffodil/ibm-tests/"

-  val tdml_01 = testDir_01 + "dpaext1.tdml"

-  lazy val runner_01 = new DFDLTestSuite(Misc.getRequiredResource(tdml_01))

-

-  @Test def test_delimiter_12_01() { runner_01.runOneTest("delimiter_12_01") }

-  //@Test def test_delimiter_12_02() { runner_01.runOneTest("delimiter_12_02") }

-  @Test def test_delimiter_12_03() { runner_01.runOneTest("delimiter_12_03") }

-  @Test def test_delimiter_12_04() { runner_01.runOneTest("delimiter_12_04") }

-

-  val testDir_02 = "/edu/illinois/ncsa/daffodil/section12/delimiter_properties/"

-  val tdml_02 = testDir_02 + "DelimiterProperties.tdml"

-  lazy val runner_02 = new DFDLTestSuite(Misc.getRequiredResource(tdml_02))

-

-  @Test def test_DelimProp_01() = { runner_02.runOneTest("DelimProp_01") }

-  @Test def testParseSequence4() { runner_02.runOneTest("ParseSequence4") }

-  @Test def testParseSequence5() { runner_02.runOneTest("ParseSequence5") }

-  //@Test def testParseSequence_4a() { runner_02.runOneTest("ParseSequence_4a") }

-  @Test def test_DelimProp_02() { runner_02.runOneTest("DelimProp_02") }

-  @Test def test_DelimProp_03() { runner_02.runOneTest("DelimProp_03") }

-  @Test def test_DelimProp_04() { runner_02.runOneTest("DelimProp_04") }

-  @Test def test_DelimProp_05() { runner_02.runOneTest("DelimProp_05") }

-  @Test def test_DelimProp_06() { runner_02.runOneTest("DelimProp_06") }

-  @Test def test_DelimProp_07() { runner_02.runOneTest("DelimProp_07") }

-  @Test def test_initiatedContentSimple1() { runner_02.runOneTest("initiatedContentSimple1") }

-  @Test def test_Lesson4_initiators_terminators() { runner_02.runOneTest("Lesson4_initiators_terminators") }

-

-  @Test def test_DelimProp_10() = {

-    runner_02.runOneTest("DelimProp_10")

-  }

-  @Test def test_DelimProp_10_01() = {

-    runner_02.runOneTest("DelimProp_10_01")

-  }

-

-  @Test def test_E1() = {

-    runner_02.runOneTest("E1")

-  }

-}

+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+
+class TestDelimiterProperties {
+
+  val testDir_01 = "/edu/illinois/ncsa/daffodil/ibm-tests/"
+  val tdml_01 = testDir_01 + "dpaext1.tdml"
+  lazy val runner_01 = new DFDLTestSuite(Misc.getRequiredResource(tdml_01))
+
+  @Test def test_delimiter_12_01() { runner_01.runOneTest("delimiter_12_01") }
+  //@Test def test_delimiter_12_02() { runner_01.runOneTest("delimiter_12_02") }
+  @Test def test_delimiter_12_03() { runner_01.runOneTest("delimiter_12_03") }
+  @Test def test_delimiter_12_04() { runner_01.runOneTest("delimiter_12_04") }
+
+  val testDir_02 = "/edu/illinois/ncsa/daffodil/section12/delimiter_properties/"
+  val tdml_02 = testDir_02 + "DelimiterProperties.tdml"
+  lazy val runner_02 = new DFDLTestSuite(Misc.getRequiredResource(tdml_02))
+
+  @Test def test_DelimProp_01() = { runner_02.runOneTest("DelimProp_01") }
+  @Test def testParseSequence4() { runner_02.runOneTest("ParseSequence4") }
+  @Test def testParseSequence5() { runner_02.runOneTest("ParseSequence5") }
+  //@Test def testParseSequence_4a() { runner_02.runOneTest("ParseSequence_4a") }
+  @Test def test_DelimProp_02() { runner_02.runOneTest("DelimProp_02") }
+  @Test def test_DelimProp_03() { runner_02.runOneTest("DelimProp_03") }
+  @Test def test_DelimProp_04() { runner_02.runOneTest("DelimProp_04") }
+  @Test def test_DelimProp_05() { runner_02.runOneTest("DelimProp_05") }
+  @Test def test_DelimProp_06() { runner_02.runOneTest("DelimProp_06") }
+  @Test def test_DelimProp_07() { runner_02.runOneTest("DelimProp_07") }
+  @Test def test_initiatedContentSimple1() { runner_02.runOneTest("initiatedContentSimple1") }
+  @Test def test_Lesson4_initiators_terminators() { runner_02.runOneTest("Lesson4_initiators_terminators") }
+
+  @Test def test_DelimProp_10() = {
+    runner_02.runOneTest("DelimProp_10")
+  }
+  @Test def test_DelimProp_10_01() = {
+    runner_02.runOneTest("DelimProp_10_01")
+  }
+
+  @Test def test_E1() = {
+    runner_02.runOneTest("E1")
+  }
+}
diff --git a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindImplicit.scala b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindImplicit.scala
index 97eaa56..cdc66b0 100644
--- a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindImplicit.scala
+++ b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section12/lengthKind/TestLengthKindImplicit.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section12.lengthKind

-

+package edu.illinois.ncsa.daffodil.section12.lengthKind
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,26 +30,26 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-

-class TestLengthKindImplicit {

-  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"

-  val tdml = testDir + "implicit.tdml"

-  lazy val runner_01 = new DFDLTestSuite(Misc.getRequiredResource(tdml))

-

-  @Test def test_nested_seq() { runner_01.runOneTest("nested_seq") }

-  @Test def test_nested_seq_01() { runner_01.runOneTest("nested_seq_01") }

-

-  @Test def test_implicit_with_len() { runner_01.runOneTest("implicit_with_len") }

-

-}

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+
+class TestLengthKindImplicit {
+  val testDir = "/edu/illinois/ncsa/daffodil/section12/lengthKind/"
+  val tdml = testDir + "implicit.tdml"
+  lazy val runner_01 = new DFDLTestSuite(Misc.getRequiredResource(tdml))
+
+  @Test def test_nested_seq() { runner_01.runOneTest("nested_seq") }
+  @Test def test_nested_seq_01() { runner_01.runOneTest("nested_seq_01") }
+
+  @Test def test_implicit_with_len() { runner_01.runOneTest("implicit_with_len") }
+
+}
diff --git a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section15/choice_groups/TestChoice.scala b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section15/choice_groups/TestChoice.scala
index f5454b6..40ecd62 100644
--- a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section15/choice_groups/TestChoice.scala
+++ b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section15/choice_groups/TestChoice.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.section15.choice_groups

+package edu.illinois.ncsa.daffodil.section15.choice_groups
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,86 +32,86 @@
  * SOFTWARE.
  */
 
-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-

-class TestChoice {

-  val testDir = "/edu/illinois/ncsa/daffodil/section15/choice_groups/"

-  val aa = testDir + "choice.tdml"

-

-  @Test def test_choiceOfGroupRefs() { runnerCH.runOneTest("choiceOfGroupRefs") }

 
-  lazy val runnerCH = new DFDLTestSuite(Misc.getRequiredResource(aa))

-  @Test def test_basicChoice() { runnerCH.runOneTest("basic") }

-  @Test def test_choice2() { runnerCH.runOneTest("choice2") }

-  @Test def test_choice3() { runnerCH.runOneTest("choice3") }

-  @Test def test_choice4() { runnerCH.runOneTest("choice4") }

-  

-  @Test def test_choiceWithInitsAndTermsInt() { runnerCH.runOneTest("choiceWithInitsAndTermsInt") }

-  @Test def test_choiceWithInitsAndTermsStr() { runnerCH.runOneTest("choiceWithInitsAndTermsStr") }

-  @Test def test_choiceWithInitsAndTermsError() { runnerCH.runOneTest("choiceWithInitsAndTermsError") }

-  @Test def test_choiceWithInitsAndTermsSeqInt() { runnerCH.runOneTest("choiceWithInitsAndTermsSeqInt") }

-  @Test def test_choiceWithInitsAndTermsSeqStr() { runnerCH.runOneTest("choiceWithInitsAndTermsSeqStr") }

-  @Test def test_nestedChoiceWithInitsAndTermsNestedInt() { runnerCH.runOneTest("nestedChoiceWithInitsAndTermsNestedInt") }
-  @Test def test_nestedChoiceWithInitsAndTermsNestedStr() { runnerCH.runOneTest("nestedChoiceWithInitsAndTermsNestedStr") }

-  @Test def test_nestedChoiceWithInitsAndTermsInt() { runnerCH.runOneTest("nestedChoiceWithInitsAndTermsInt") }

+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+
+class TestChoice {
+  val testDir = "/edu/illinois/ncsa/daffodil/section15/choice_groups/"
+  val aa = testDir + "choice.tdml"
+
+  @Test def test_choiceOfGroupRefs() { runnerCH.runOneTest("choiceOfGroupRefs") }
+
+  lazy val runnerCH = new DFDLTestSuite(Misc.getRequiredResource(aa))
+  @Test def test_basicChoice() { runnerCH.runOneTest("basic") }
+  @Test def test_choice2() { runnerCH.runOneTest("choice2") }
+  @Test def test_choice3() { runnerCH.runOneTest("choice3") }
+  @Test def test_choice4() { runnerCH.runOneTest("choice4") }
   
-  @Test def test_choiceWithSequence1() { runnerCH.runOneTest("choiceWithSequence1") }

-  @Test def test_choiceWithSequence2() { runnerCH.runOneTest("choiceWithSequence2") }

-  @Test def test_choiceWithChoiceInt() { runnerCH.runOneTest("choiceWithChoiceInt") }

-  @Test def test_choiceWithChoiceFloat() { runnerCH.runOneTest("choiceWithChoiceFloat") }

-  @Test def test_choiceWithChoiceString() { runnerCH.runOneTest("choiceWithChoiceString") }

-  @Test def test_choiceWithArrayInt() { runnerCH.runOneTest("choiceWithArrayInts") }

-  @Test def test_choiceWithArrayFloat() { runnerCH.runOneTest("choiceWithArrayFloats") }

-  @Test def test_choiceWithArrayString() { runnerCH.runOneTest("choiceWithChoiceString") }

-

-  @Test def test_choice5() { runnerCH.runOneTest("choice5") }

-  @Test def test_choice6() { runnerCH.runOneTest("choice6") }

-  @Test def test_choiceFail1() { runnerCH.runOneTest("choiceFail1") }

-  @Test def test_choiceDelim1() {

-    // Logging@Test defaults.setLoggingLevel(LogLevel.Debug)

-    runnerCH.runOneTest("choiceDelim1")

-  }

-  @Test def test_choiceDelim2() { runnerCH.runOneTest("choiceDelim2") }

-  @Test def test_choiceDelimFloat() { runnerCH.runOneTest("choiceDelimFloat") }

-  @Test def test_choiceDelimString() { runnerCH.runOneTest("choiceDelimString") }

-  @Test def test_choiceDelimStringwSp() { runnerCH.runOneTest("choiceDelimStringwSp") }

-  @Test def test_choiceDelimInt() { runnerCH.runOneTest("choiceDelimInt") }

-

-  @Test def test_nestedChoice1() {

-    // Logging@Test defaults.setLoggingLevel(LogLevel.Debug)

-    runnerCH.runOneTest("nestedChoice1")

-  }

-

-  @Test def test_nestedChoiceAllString() { runnerCH.runOneTest("nestedChoiceAllString") }

-  @Test def test_nestedChoiceAllFloat() { runnerCH.runOneTest("nestedChoiceAllFloat") }

-  @Test def test_nestedChoiceAllInt() { runnerCH.runOneTest("nestedChoiceAllInt") }

-

-  @Test def test_choiceInSeq1() { runnerCH.runOneTest("choiceInSequenceWithSeparators1") }

-  @Test def test_choiceInSeq2() { runnerCH.runOneTest("choiceInSequenceWithSeparators2") }

-

-  @Test def test_seqInChoiceInSeq1() { runnerCH.runOneTest("sequenceInChoiceInSequenceWithSeparators1") }

-  @Test def test_seqInChoiceInSeq2() { runnerCH.runOneTest("sequenceInChoiceInSequenceWithSeparators2") }

-  @Test def test_seqInChoiceInSeq3() { runnerCH.runOneTest("sequenceInChoiceInSequenceWithSameSeparators1") }

-  @Test def test_seqInChoiceInSeq4() { runnerCH.runOneTest("sequenceInChoiceInSequenceWithSameSeparators2") }

-

+  @Test def test_choiceWithInitsAndTermsInt() { runnerCH.runOneTest("choiceWithInitsAndTermsInt") }
+  @Test def test_choiceWithInitsAndTermsStr() { runnerCH.runOneTest("choiceWithInitsAndTermsStr") }
+  @Test def test_choiceWithInitsAndTermsError() { runnerCH.runOneTest("choiceWithInitsAndTermsError") }
+  @Test def test_choiceWithInitsAndTermsSeqInt() { runnerCH.runOneTest("choiceWithInitsAndTermsSeqInt") }
+  @Test def test_choiceWithInitsAndTermsSeqStr() { runnerCH.runOneTest("choiceWithInitsAndTermsSeqStr") }
+  @Test def test_nestedChoiceWithInitsAndTermsNestedInt() { runnerCH.runOneTest("nestedChoiceWithInitsAndTermsNestedInt") }
+  @Test def test_nestedChoiceWithInitsAndTermsNestedStr() { runnerCH.runOneTest("nestedChoiceWithInitsAndTermsNestedStr") }
+  @Test def test_nestedChoiceWithInitsAndTermsInt() { runnerCH.runOneTest("nestedChoiceWithInitsAndTermsInt") }
+  
+  @Test def test_choiceWithSequence1() { runnerCH.runOneTest("choiceWithSequence1") }
+  @Test def test_choiceWithSequence2() { runnerCH.runOneTest("choiceWithSequence2") }
+  @Test def test_choiceWithChoiceInt() { runnerCH.runOneTest("choiceWithChoiceInt") }
+  @Test def test_choiceWithChoiceFloat() { runnerCH.runOneTest("choiceWithChoiceFloat") }
+  @Test def test_choiceWithChoiceString() { runnerCH.runOneTest("choiceWithChoiceString") }
+  @Test def test_choiceWithArrayInt() { runnerCH.runOneTest("choiceWithArrayInts") }
+  @Test def test_choiceWithArrayFloat() { runnerCH.runOneTest("choiceWithArrayFloats") }
+  @Test def test_choiceWithArrayString() { runnerCH.runOneTest("choiceWithChoiceString") }
+
+  @Test def test_choice5() { runnerCH.runOneTest("choice5") }
+  @Test def test_choice6() { runnerCH.runOneTest("choice6") }
+  @Test def test_choiceFail1() { runnerCH.runOneTest("choiceFail1") }
+  @Test def test_choiceDelim1() {
+    // Logging@Test defaults.setLoggingLevel(LogLevel.Debug)
+    runnerCH.runOneTest("choiceDelim1")
+  }
+  @Test def test_choiceDelim2() { runnerCH.runOneTest("choiceDelim2") }
+  @Test def test_choiceDelimFloat() { runnerCH.runOneTest("choiceDelimFloat") }
+  @Test def test_choiceDelimString() { runnerCH.runOneTest("choiceDelimString") }
+  @Test def test_choiceDelimStringwSp() { runnerCH.runOneTest("choiceDelimStringwSp") }
+  @Test def test_choiceDelimInt() { runnerCH.runOneTest("choiceDelimInt") }
+
+  @Test def test_nestedChoice1() {
+    // Logging@Test defaults.setLoggingLevel(LogLevel.Debug)
+    runnerCH.runOneTest("nestedChoice1")
+  }
+
+  @Test def test_nestedChoiceAllString() { runnerCH.runOneTest("nestedChoiceAllString") }
+  @Test def test_nestedChoiceAllFloat() { runnerCH.runOneTest("nestedChoiceAllFloat") }
+  @Test def test_nestedChoiceAllInt() { runnerCH.runOneTest("nestedChoiceAllInt") }
+
+  @Test def test_choiceInSeq1() { runnerCH.runOneTest("choiceInSequenceWithSeparators1") }
+  @Test def test_choiceInSeq2() { runnerCH.runOneTest("choiceInSequenceWithSeparators2") }
+
+  @Test def test_seqInChoiceInSeq1() { runnerCH.runOneTest("sequenceInChoiceInSequenceWithSeparators1") }
+  @Test def test_seqInChoiceInSeq2() { runnerCH.runOneTest("sequenceInChoiceInSequenceWithSeparators2") }
+  @Test def test_seqInChoiceInSeq3() { runnerCH.runOneTest("sequenceInChoiceInSequenceWithSameSeparators1") }
+  @Test def test_seqInChoiceInSeq4() { runnerCH.runOneTest("sequenceInChoiceInSequenceWithSameSeparators2") }
+
   @Test def test_choice_minOccurs() { runnerCH.runOneTest("choice_minOccurs") }
   @Test def test_choice_maxOccurs() { runnerCH.runOneTest("choice_maxOccurs") }
-

-  val testDir1 = "/edu/illinois/ncsa/daffodil/ibm-tests/"

-  val tdml1 = testDir1 + "dpaext2.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml1))

-  

-  @Test def test_choices_basic_15_01() { runner.runOneTest("choices_basic_15_01") }

-  @Test def test_choices_basic_15_02() { runner.runOneTest("choices_basic_15_02") }

-  @Test def test_choices_basic_15_03() { runner.runOneTest("choices_basic_15_03") }

-}

+
+  val testDir1 = "/edu/illinois/ncsa/daffodil/ibm-tests/"
+  val tdml1 = testDir1 + "dpaext2.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml1))
+  
+  @Test def test_choices_basic_15_01() { runner.runOneTest("choices_basic_15_01") }
+  @Test def test_choices_basic_15_02() { runner.runOneTest("choices_basic_15_02") }
+  @Test def test_choices_basic_15_03() { runner.runOneTest("choices_basic_15_03") }
+}
diff --git a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section16/array_optional_elem/TestArrayOptionalElem.scala b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section16/array_optional_elem/TestArrayOptionalElem.scala
index 97b2d36..a3ae845 100644
--- a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section16/array_optional_elem/TestArrayOptionalElem.scala
+++ b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section16/array_optional_elem/TestArrayOptionalElem.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.section16.array_optional_elem

+package edu.illinois.ncsa.daffodil.section16.array_optional_elem
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,42 +32,42 @@
  * SOFTWARE.
  */
 
-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-

-class TestArrayOptionalElem {

-  val testDir = "/edu/illinois/ncsa/daffodil/section16/array_optional_elem/"

-  val aa = testDir + "ArrayOptionalElem.tdml"

-

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))

-  

-  @Test def test_error01() { runner.runOneTest("error01") }

-  @Test def test_postfixNoErr() { runner.runOneTest("postfixNoErr") }

-

-  @Test def test_optionalElem() { runner.runOneTest("optionalElem") }

-  @Test def test_optionalWithSeparators() { runner.runOneTest("optionalWithSeparators") }

-  @Test def test_Lesson6_optional_element() { runner.runOneTest("Lesson6_optional_element") }

-  @Test def test_Lesson6_optional_element_01() { runner.runOneTest("Lesson6_optional_element_01") }

-  @Test def test_Lesson6_fixed_array() { runner.runOneTest("Lesson6_fixed_array") }

-  @Test def test_Lesson6_variable_array() { runner.runOneTest("Lesson6_variable_array") }

-  @Test def test_Lesson6_variable_array_01() { runner.runOneTest("Lesson6_variable_array_01") }

-  @Test def test_Lesson6_variable_array_02() { runner.runOneTest("Lesson6_variable_array_02") }

-

-  val testDir01 = "/edu/illinois/ncsa/daffodil/section05/facets/"

-  val ab = testDir01 + "Facets.tdml"

-  lazy val runner01 = new DFDLTestSuite(Misc.getRequiredResource(ab))

-  @Test def test_leftOverData_Neg() { runner01.runOneTest("leftOverData_Neg") }

-  

-  val testDir1 = "/edu/illinois/ncsa/daffodil/ibm-tests/"

-  val tdml1 = testDir1 + "dpaext2.tdml"

-  lazy val runner1 = new DFDLTestSuite(Misc.getRequiredResource(tdml1))

-  @Test def test_arrays_16_01() { runner1.runOneTest("arrays_16_01") }

-}

+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+
+class TestArrayOptionalElem {
+  val testDir = "/edu/illinois/ncsa/daffodil/section16/array_optional_elem/"
+  val aa = testDir + "ArrayOptionalElem.tdml"
+
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(aa))
+  
+  @Test def test_error01() { runner.runOneTest("error01") }
+  @Test def test_postfixNoErr() { runner.runOneTest("postfixNoErr") }
+
+  @Test def test_optionalElem() { runner.runOneTest("optionalElem") }
+  @Test def test_optionalWithSeparators() { runner.runOneTest("optionalWithSeparators") }
+  @Test def test_Lesson6_optional_element() { runner.runOneTest("Lesson6_optional_element") }
+  @Test def test_Lesson6_optional_element_01() { runner.runOneTest("Lesson6_optional_element_01") }
+  @Test def test_Lesson6_fixed_array() { runner.runOneTest("Lesson6_fixed_array") }
+  @Test def test_Lesson6_variable_array() { runner.runOneTest("Lesson6_variable_array") }
+  @Test def test_Lesson6_variable_array_01() { runner.runOneTest("Lesson6_variable_array_01") }
+  @Test def test_Lesson6_variable_array_02() { runner.runOneTest("Lesson6_variable_array_02") }
+
+  val testDir01 = "/edu/illinois/ncsa/daffodil/section05/facets/"
+  val ab = testDir01 + "Facets.tdml"
+  lazy val runner01 = new DFDLTestSuite(Misc.getRequiredResource(ab))
+  @Test def test_leftOverData_Neg() { runner01.runOneTest("leftOverData_Neg") }
+  
+  val testDir1 = "/edu/illinois/ncsa/daffodil/ibm-tests/"
+  val tdml1 = testDir1 + "dpaext2.tdml"
+  lazy val runner1 = new DFDLTestSuite(Misc.getRequiredResource(tdml1))
+  @Test def test_arrays_16_01() { runner1.runOneTest("arrays_16_01") }
+}
diff --git a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressions.scala b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressions.scala
index 41f3405..99e955f 100644
--- a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressions.scala
+++ b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/TestDFDLExpressions.scala
@@ -1,5 +1,5 @@
-package edu.illinois.ncsa.daffodil.section23.dfdl_expressions

-

+package edu.illinois.ncsa.daffodil.section23.dfdl_expressions
+
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
  * Developed by: Tresys Technology, LLC
@@ -30,124 +30,124 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
  * SOFTWARE.
- */

-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-import edu.illinois.ncsa.daffodil.debugger.Debugger

-

-class TestDFDLExpressions {

-  val testDir = "/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/"

-  val tdml = testDir + "expressions.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml), validateTDMLFile = false)

-

-  @Test def test_regexLookahead() { runner.runOneTest("regexLookahead") }

-  @Test def test_regexLookaheadFail() { runner.runOneTest("regexLookaheadFail") }

-  @Test def test_regexLookaheadFail2() { runner.runOneTest("regexLookaheadFail2") }

+ */
+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+import edu.illinois.ncsa.daffodil.debugger.Debugger
+
+class TestDFDLExpressions {
+  val testDir = "/edu/illinois/ncsa/daffodil/section23/dfdl_expressions/"
+  val tdml = testDir + "expressions.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml), validateTDMLFile = false)
+
+  @Test def test_regexLookahead() { runner.runOneTest("regexLookahead") }
+  @Test def test_regexLookaheadFail() { runner.runOneTest("regexLookaheadFail") }
+  @Test def test_regexLookaheadFail2() { runner.runOneTest("regexLookaheadFail2") }
   //  @Test def test_regexCompatFail() { runner.runOneTest("regexCompatFail") }
-

-  @Test def test_expressionRules01() { runner.runOneTest("expressionRules01") }

-  @Test def test_expressionRules02() { runner.runOneTest("expressionRules02") }

+
+  @Test def test_expressionRules01() { runner.runOneTest("expressionRules01") }
+  @Test def test_expressionRules02() { runner.runOneTest("expressionRules02") }
 
 //  DFDL-667  
 //  @Test def test_expressionRules03() { runner.runOneTest("expressionRules03") }
 //  @Test def test_expressionRules04() { runner.runOneTest("expressionRules04") }
-

-  @Test def test_expressions_lke3_rel() { runner.runOneTest("lke3_rel") }

-  @Test def test_expressions_lke1_rel() { runner.runOneTest("lke1_rel") }

-  @Test def test_expressions_lke1_abs() { runner.runOneTest("lke1_abs") }

-  @Test def test_expressions_ocke1() { runner.runOneTest("ocke1") }

-  @Test def test_expressions_ocke2() { runner.runOneTest("ocke2") }

-  @Test def test_ArrayOptElem_01() { runner.runOneTest("ArrayOptElem_01") }

-  @Test def test_expressions_lke2_rel() { runner.runOneTest("lke2_rel") }

-  @Test def test_expression_type_error1() { runner.runOneTest("expression-type-error1") }

-  @Test def test_expression_type_error2() { runner.runOneTest("expression-type-error2") }

-  @Test def test_expression_type_error3() { runner.runOneTest("expression-type-error3") }

-  @Test def test_expression_type_error4() { runner.runOneTest("expression-type-error4") }

-  @Test def test_expression_unknown_prefix() { runner.runOneTest("expression-unknown-prefix") }

-  @Test def test_ocke_rel() { runner.runOneTest("ocke_rel") }

-  @Test def test_expresion_bad_path_to_element() { runner.runOneTest("expresion_bad_path_to_element") }

-  @Test def test_ArrayOptElem_02() { runner.runOneTest("ArrayOptElem_02") }

-  @Test def test_checkConstraints() { runner.runOneTest("dfdlCheckConstraints") }

-

-  @Test def test_nonFunctionIsDetected() = { runner.runOneTest("nonFunctionIsDetected") }

-  @Test def test_constantFunction1() { runner.runOneTest("constantFunction1") }

-  @Test def test_dfdlPosition1() { runner.runOneTest("dfdlPosition1") }

-  @Test def test_dfdlPosition2() { runner.runOneTest("dfdlPosition2") }

-  @Test def test_dfdlPosition3() { runner.runOneTest("dfdlPosition3") }

-  @Test def test_dfdlPosition4() { runner.runOneTest("dfdlPosition4") }

-  @Test def test_dfdlPosition5() { runner.runOneTest("dfdlPosition5") }

-

-  @Test def test_repeatFlags1() { runner.runOneTest("repeatFlags1") }

-  @Test def test_repeatFlags2() { runner.runOneTest("repeatFlags2") }

-  @Test def test_repeatFlags3() { runner.runOneTest("repeatFlags3") }

-  @Test def test_repeatFlags4() { runner.runOneTest("repeatFlags4") }

-  @Test def test_repeatFlags5() { runner.runOneTest("repeatFlags5") }

-

-  @Test def test_repeatBitFlags1() { runner.runOneTest("repeatBitFlags1") }

-  @Test def test_repeatBitFlags2() { runner.runOneTest("repeatBitFlags2") }

-  @Test def test_repeatBitFlags3() { runner.runOneTest("repeatBitFlags3") }

-  @Test def test_repeatBitFlags4() { runner.runOneTest("repeatBitFlags4") }

-  @Test def test_repeatBitFlags5() { runner.runOneTest("repeatBitFlags5") }

-  @Test def test_repeatBitFlags6() { runner.runOneTest("repeatBitFlags6") }

-

-  @Test def test_invalid_enum_1() { runner.runOneTest("invalid_enum_1") }

-  @Test def test_invalid_enum_2() { runner.runOneTest("invalid_enum_2") }

-  @Test def test_invalid_enum_3() { runner.runOneTest("invalid_enum_3") }

+
+  @Test def test_expressions_lke3_rel() { runner.runOneTest("lke3_rel") }
+  @Test def test_expressions_lke1_rel() { runner.runOneTest("lke1_rel") }
+  @Test def test_expressions_lke1_abs() { runner.runOneTest("lke1_abs") }
+  @Test def test_expressions_ocke1() { runner.runOneTest("ocke1") }
+  @Test def test_expressions_ocke2() { runner.runOneTest("ocke2") }
+  @Test def test_ArrayOptElem_01() { runner.runOneTest("ArrayOptElem_01") }
+  @Test def test_expressions_lke2_rel() { runner.runOneTest("lke2_rel") }
+  @Test def test_expression_type_error1() { runner.runOneTest("expression-type-error1") }
+  @Test def test_expression_type_error2() { runner.runOneTest("expression-type-error2") }
+  @Test def test_expression_type_error3() { runner.runOneTest("expression-type-error3") }
+  @Test def test_expression_type_error4() { runner.runOneTest("expression-type-error4") }
+  @Test def test_expression_unknown_prefix() { runner.runOneTest("expression-unknown-prefix") }
+  @Test def test_ocke_rel() { runner.runOneTest("ocke_rel") }
+  @Test def test_expresion_bad_path_to_element() { runner.runOneTest("expresion_bad_path_to_element") }
+  @Test def test_ArrayOptElem_02() { runner.runOneTest("ArrayOptElem_02") }
+  @Test def test_checkConstraints() { runner.runOneTest("dfdlCheckConstraints") }
+
+  @Test def test_nonFunctionIsDetected() = { runner.runOneTest("nonFunctionIsDetected") }
+  @Test def test_constantFunction1() { runner.runOneTest("constantFunction1") }
+  @Test def test_dfdlPosition1() { runner.runOneTest("dfdlPosition1") }
+  @Test def test_dfdlPosition2() { runner.runOneTest("dfdlPosition2") }
+  @Test def test_dfdlPosition3() { runner.runOneTest("dfdlPosition3") }
+  @Test def test_dfdlPosition4() { runner.runOneTest("dfdlPosition4") }
+  @Test def test_dfdlPosition5() { runner.runOneTest("dfdlPosition5") }
+
+  @Test def test_repeatFlags1() { runner.runOneTest("repeatFlags1") }
+  @Test def test_repeatFlags2() { runner.runOneTest("repeatFlags2") }
+  @Test def test_repeatFlags3() { runner.runOneTest("repeatFlags3") }
+  @Test def test_repeatFlags4() { runner.runOneTest("repeatFlags4") }
+  @Test def test_repeatFlags5() { runner.runOneTest("repeatFlags5") }
+
+  @Test def test_repeatBitFlags1() { runner.runOneTest("repeatBitFlags1") }
+  @Test def test_repeatBitFlags2() { runner.runOneTest("repeatBitFlags2") }
+  @Test def test_repeatBitFlags3() { runner.runOneTest("repeatBitFlags3") }
+  @Test def test_repeatBitFlags4() { runner.runOneTest("repeatBitFlags4") }
+  @Test def test_repeatBitFlags5() { runner.runOneTest("repeatBitFlags5") }
+  @Test def test_repeatBitFlags6() { runner.runOneTest("repeatBitFlags6") }
+
+  @Test def test_invalid_enum_1() { runner.runOneTest("invalid_enum_1") }
+  @Test def test_invalid_enum_2() { runner.runOneTest("invalid_enum_2") }
+  @Test def test_invalid_enum_3() { runner.runOneTest("invalid_enum_3") }
 
   @Test def test_trueFalseTypeError() { runner.runOneTest("trueFalseTypeError") }
   @Test def test_trueFalseTypeCorrect() { runner.runOneTest("trueFalseTypeCorrect") }
-

-  val tdml2 = testDir + "functions.tdml"

-  lazy val runner_fun = new DFDLTestSuite(Misc.getRequiredResource(tdml2))

-

-  @Test def test_dateTimeFunctions01() { runner_fun.runOneTest("dateTimeFunctions01") }

-  @Test def test_dateTimeFunctions02() { runner_fun.runOneTest("dateTimeFunctions02") }

-  @Test def test_dateFunctions01() { runner_fun.runOneTest("dateFunctions01") }

-  @Test def test_dateFunctions02() { runner_fun.runOneTest("dateFunctions02") }

-  @Test def test_timeFunctions01() { runner_fun.runOneTest("timeFunctions01") }

-  @Test def test_timeFunctions02() { runner_fun.runOneTest("timeFunctions02") }

-  @Test def test_functionFail01() { runner_fun.runOneTest("functionFail01") }

-  @Test def test_functionFail02() { runner_fun.runOneTest("functionFail02") }

-  @Test def test_functionFail03() { runner_fun.runOneTest("functionFail03") }

-

-  val testDir2 = "/edu/illinois/ncsa/daffodil/section23/dfdl_functions/"

-  val aa = testDir2 + "Functions.tdml"

-  lazy val runner2 = new DFDLTestSuite(Misc.getRequiredResource(aa))

-

-  @Test def test_testBit_0() { runner2.runOneTest("testBit_0") }

-  @Test def test_testBit_1() { runner2.runOneTest("testBit_1") }

+
+  val tdml2 = testDir + "functions.tdml"
+  lazy val runner_fun = new DFDLTestSuite(Misc.getRequiredResource(tdml2))
+
+  @Test def test_dateTimeFunctions01() { runner_fun.runOneTest("dateTimeFunctions01") }
+  @Test def test_dateTimeFunctions02() { runner_fun.runOneTest("dateTimeFunctions02") }
+  @Test def test_dateFunctions01() { runner_fun.runOneTest("dateFunctions01") }
+  @Test def test_dateFunctions02() { runner_fun.runOneTest("dateFunctions02") }
+  @Test def test_timeFunctions01() { runner_fun.runOneTest("timeFunctions01") }
+  @Test def test_timeFunctions02() { runner_fun.runOneTest("timeFunctions02") }
+  @Test def test_functionFail01() { runner_fun.runOneTest("functionFail01") }
+  @Test def test_functionFail02() { runner_fun.runOneTest("functionFail02") }
+  @Test def test_functionFail03() { runner_fun.runOneTest("functionFail03") }
+
+  val testDir2 = "/edu/illinois/ncsa/daffodil/section23/dfdl_functions/"
+  val aa = testDir2 + "Functions.tdml"
+  lazy val runner2 = new DFDLTestSuite(Misc.getRequiredResource(aa))
+
+  @Test def test_testBit_0() { runner2.runOneTest("testBit_0") }
+  @Test def test_testBit_1() { runner2.runOneTest("testBit_1") }
   @Test def test_testBit_2() { runner2.runOneTest("testBit_2") }
   // This test should be giving a runtime SDE instead of parse error (DFDL-710)
-  // @Test def test_testBit_3() { runner2.runOneTest("testBit_3") }

-

-  @Test def test_stringLiteralFromString_0() { runner2.runOneTest("stringLiteralFromString_0") }

-  @Test def test_stringLiteralFromString_1() { runner2.runOneTest("stringLiteralFromString_1") }

-  @Test def test_stringLiteralFromString_2() { runner2.runOneTest("stringLiteralFromString_2") }

-

-  @Test def test_setBits_0() { runner2.runOneTest("setBits_0") }

-  @Test def test_setBits_1() { runner2.runOneTest("setBits_1") }

-  @Test def test_setBits_2() { runner2.runOneTest("setBits_2") }

-

-  @Test def test_containsEntity_0() { runner2.runOneTest("containsEntity_0") }

-  @Test def test_containsEntity_1() { runner2.runOneTest("containsEntity_1") }

-  @Test def test_containsEntity_2() { runner2.runOneTest("containsEntity_2") }

-  @Test def test_containsEntity_3() { runner2.runOneTest("containsEntity_3") }

-  @Test def test_containsEntity_4() { runner2.runOneTest("containsEntity_4") }

-

-  @Test def test_occursCount_0() { runner2.runOneTest("occursCount_0") }

-

+  // @Test def test_testBit_3() { runner2.runOneTest("testBit_3") }
+
+  @Test def test_stringLiteralFromString_0() { runner2.runOneTest("stringLiteralFromString_0") }
+  @Test def test_stringLiteralFromString_1() { runner2.runOneTest("stringLiteralFromString_1") }
+  @Test def test_stringLiteralFromString_2() { runner2.runOneTest("stringLiteralFromString_2") }
+
+  @Test def test_setBits_0() { runner2.runOneTest("setBits_0") }
+  @Test def test_setBits_1() { runner2.runOneTest("setBits_1") }
+  @Test def test_setBits_2() { runner2.runOneTest("setBits_2") }
+
+  @Test def test_containsEntity_0() { runner2.runOneTest("containsEntity_0") }
+  @Test def test_containsEntity_1() { runner2.runOneTest("containsEntity_1") }
+  @Test def test_containsEntity_2() { runner2.runOneTest("containsEntity_2") }
+  @Test def test_containsEntity_3() { runner2.runOneTest("containsEntity_3") }
+  @Test def test_containsEntity_4() { runner2.runOneTest("containsEntity_4") }
+
+  @Test def test_occursCount_0() { runner2.runOneTest("occursCount_0") }
+
   //  @Test def test_valueLength_0() { runner2.runOneTest("valueLength_0") }
   //  @Test def test_valueLength_1() { runner2.runOneTest("valueLength_1") }
-

+
   //  @Test def test_contentLength_0() { runner2.runOneTest("contentLength_0") }
   //  @Test def test_contentLength_1() { runner2.runOneTest("contentLength_1") }
-

-}

+
+}
diff --git a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section24/regular_expressions/TestRegularExpressions.scala b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section24/regular_expressions/TestRegularExpressions.scala
index 452e4d6..f7f7fa0 100644
--- a/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section24/regular_expressions/TestRegularExpressions.scala
+++ b/daffodil-test/src/test/scala/edu/illinois/ncsa/daffodil/section24/regular_expressions/TestRegularExpressions.scala
@@ -1,4 +1,4 @@
-package edu.illinois.ncsa.daffodil.section24.regular_expressions

+package edu.illinois.ncsa.daffodil.section24.regular_expressions
 
 /* Copyright (c) 2012-2013 Tresys Technology, LLC. All rights reserved.
  *
@@ -32,29 +32,29 @@
  * SOFTWARE.
  */
 
-

-import junit.framework.Assert._

-import org.junit.Test

-import scala.xml._

-import edu.illinois.ncsa.daffodil.xml.XMLUtils

-import edu.illinois.ncsa.daffodil.xml.XMLUtils._

-import edu.illinois.ncsa.daffodil.compiler.Compiler

-import edu.illinois.ncsa.daffodil.util._

-import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite

-import java.io.File

-

-class TestRegularExpressions {

-  val testDir = "/edu/illinois/ncsa/daffodil/section24/regular_expressions/"

-  val tdml = testDir + "RegularExpressions.tdml"

-  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml))

-

-  @Test def testRegEx_01() { runner.runOneTest("testRegEx_01") }

-  @Test def testRegEx_02() { runner.runOneTest("testRegEx_02") }

-  @Test def testRegEx_03() { runner.runOneTest("testRegEx_03") }

+
+import junit.framework.Assert._
+import org.junit.Test
+import scala.xml._
+import edu.illinois.ncsa.daffodil.xml.XMLUtils
+import edu.illinois.ncsa.daffodil.xml.XMLUtils._
+import edu.illinois.ncsa.daffodil.compiler.Compiler
+import edu.illinois.ncsa.daffodil.util._
+import edu.illinois.ncsa.daffodil.tdml.DFDLTestSuite
+import java.io.File
+
+class TestRegularExpressions {
+  val testDir = "/edu/illinois/ncsa/daffodil/section24/regular_expressions/"
+  val tdml = testDir + "RegularExpressions.tdml"
+  lazy val runner = new DFDLTestSuite(Misc.getRequiredResource(tdml))
+
+  @Test def testRegEx_01() { runner.runOneTest("testRegEx_01") }
+  @Test def testRegEx_02() { runner.runOneTest("testRegEx_02") }
+  @Test def testRegEx_03() { runner.runOneTest("testRegEx_03") }
 
   // // Unsupported Java 7 features (should return Schema Definition Errors)
-  // @Test def testRegEx_04() { runner.runOneTest("testRegEx_04") }

-  // @Test def testRegEx_05() { runner.runOneTest("testRegEx_05") }

-  // @Test def testRegEx_06() { runner.runOneTest("testRegEx_06") }

-  // @Test def testRegEx_07() { runner.runOneTest("testRegEx_07") }

+  // @Test def testRegEx_04() { runner.runOneTest("testRegEx_04") }
+  // @Test def testRegEx_05() { runner.runOneTest("testRegEx_05") }
+  // @Test def testRegEx_06() { runner.runOneTest("testRegEx_06") }
+  // @Test def testRegEx_07() { runner.runOneTest("testRegEx_07") }
 }