summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBurak Emir <emir@epfl.ch>2006-09-07 16:54:57 +0000
committerBurak Emir <emir@epfl.ch>2006-09-07 16:54:57 +0000
commitff389f693c06fb5db50c17b8d7d3c7a3b505420f (patch)
tree98a2a1cde5b5ca735f890fed87e94647632fd07a
parentfe6e0afa5cd320250be567ae31da77cc19e24d44 (diff)
downloadscala-ff389f693c06fb5db50c17b8d7d3c7a3b505420f.tar.gz
scala-ff389f693c06fb5db50c17b8d7d3c7a3b505420f.tar.bz2
scala-ff389f693c06fb5db50c17b8d7d3c7a3b505420f.zip
xml changes
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala55
-rw-r--r--src/library/scala/xml/Utility.scala2
-rw-r--r--test/files/jvm/xml02.check13
-rw-r--r--test/files/jvm/xml02.scala55
-rw-r--r--test/files/jvm/xml03syntax.check24
-rw-r--r--test/files/jvm/xml03syntax.scala62
-rw-r--r--test/files/jvm/xml04embed.check3
-rw-r--r--test/files/jvm/xml04embed.scala17
8 files changed, 206 insertions, 25 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala b/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala
index 5b54134575..6ba999eb11 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala
@@ -66,10 +66,11 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean
/** checks whether next character starts a Scala block, if yes, skip it.
* @return true if next character starts a scala block
*/
- /*[Duplicate]*/ def xCheckEmbeddedBlock: Boolean = {
- xEmbeddedBlock =
- enableEmbeddedExpressions && (ch == '{') && { nextch; ch != '{' }
- xEmbeddedBlock
+ /*[Duplicate]*/ def xCheckEmbeddedBlock:Boolean = {
+ // attentions, side-effect, used in xText
+ xEmbeddedBlock = ( ch == '{' ) && { nextch;( ch != '{' ) }
+ //Console.println("pos = "+pos+" xEmbeddedBlock returns "+xEmbeddedBlock)
+ return xEmbeddedBlock;
}
/** parse attribute and add it to listmap
@@ -91,7 +92,7 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean
val tmp = xAttributeValue(delim)
nextch
Literal(Constant(tmp))
- case '{' if enableEmbeddedExpressions =>
+ case '{' =>
nextch
xEmbeddedExpr
case _ =>
@@ -265,14 +266,18 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean
* @precond ch == '{'
* @postcond: xEmbeddedBlock == false!
*/
- def content_BRACE(p: Int, ts:mutable.ArrayBuffer[Tree]): Unit =
+ def content_BRACE(p: Int, ts:mutable.ArrayBuffer[Tree]): Unit = {
+ //Console.println("content_BRACE, p = "+s.currentPos)
if (xCheckEmbeddedBlock)
ts.append(xEmbeddedExpr)
else {
+ appendText(p, ts, xText)/*
val str = new StringBuffer("{")
str.append(xText)
- appendText(p, ts, str.toString())
+ nextch
+ appendText(p, ts, str.toString())*/
}
+ }
/** Returns true if it encounters an end tag (without consuming it),
* appends trees to ts as side-effect.
@@ -409,27 +414,29 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean
*/
/*[Duplicate]*/ def xText: String = {
if( xEmbeddedBlock ) Predef.error("internal error: encountered embedded block"); // assert
-
- if( xCheckEmbeddedBlock ) {
- return ""
- } else {
+ //Console.println("xText ch now "+ch)
+ //if( xCheckEmbeddedBlock ) {
+ // return ""
+ //} else {
var exit = false;
while( !exit ) {
putChar( ch );
- exit = { nextch; xCheckEmbeddedBlock }||( ch == '<' ) || ( ch == '&' );
+ val expectRBRACE = ch == '}'
+ // TODO check for "}}"
+ nextch
+ if(expectRBRACE) {
+ if(ch == '}')
+ nextch
+ else
+ s.syntaxError("in XML content, please use '}}' to express '}'")
+ }
+ exit = xCheckEmbeddedBlock ||( ch == '<' ) || ( ch == '&' );
}
val str = cbuf.toString();
cbuf.setLength( 0 );
str
- }
+ //}
}
- //override type Tree = handle.Tree;
- //override type Tree = handle.Tree;
-
- final val PATTERN = true;
- final val EXPR = false;
-
- val enableEmbeddedExpressions: Boolean = true;
//val cbuf = new StringBuffer();
@@ -532,9 +539,9 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean
EmptyTree;
}
- def xEmbeddedExpr:Tree = {
+ def xEmbeddedExpr: Tree = {
sync;
- val b = p.expr(true,false);
+ val b = p.block() //p.expr(true,false);
if(/*s.*/token != RBRACE) {
reportSyntaxError(" expected end of Scala block");
}
@@ -558,13 +565,13 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean
//var ch: Char = _;
/** this method assign the next character to ch and advances in input */
- def nextch: Unit = { s.in.next; /*s.xNext;*/ ch = s.in.ch ; pos = s.currentPos; }
+ def nextch: Unit = { s.in.next; /*s.xNext;*/ ch = s.in.ch ; pos = s.in.cpos; }
//def lookahead = { s.xLookahead }
def init: Unit = {
ch = s.in.ch;
- pos = s.currentPos;
+ pos = s.in.cpos;
//Console.println("\ninit! ch = "+ch);
}
diff --git a/src/library/scala/xml/Utility.scala b/src/library/scala/xml/Utility.scala
index ba34954362..31fd99803a 100644
--- a/src/library/scala/xml/Utility.scala
+++ b/src/library/scala/xml/Utility.scala
@@ -128,7 +128,7 @@ object Utility extends AnyRef with parsing.TokenTests {
def sequenceToXML(children: Seq[Node], pscope: NamespaceBinding, sb: StringBuilder, stripComment: Boolean): Unit = {
if(children.isEmpty)
return
- else if(children exists { y => y.isInstanceOf[Atom[Any]] }) {
+ else if(children forall { y => y.isInstanceOf[Atom[Any]] && !y.isInstanceOf[Text] }) { // add space
val it = children.elements
val f = it.next
toXML(f, f.scope, sb, stripComment)
diff --git a/test/files/jvm/xml02.check b/test/files/jvm/xml02.check
new file mode 100644
index 0000000000..dc9e15dd2b
--- /dev/null
+++ b/test/files/jvm/xml02.check
@@ -0,0 +1,13 @@
+attributes
+one
+passed ok
+two
+passed ok
+three
+passed ok
+four
+passed ok
+five
+passed ok
+patterns
+passed ok
diff --git a/test/files/jvm/xml02.scala b/test/files/jvm/xml02.scala
new file mode 100644
index 0000000000..cbc9b5041b
--- /dev/null
+++ b/test/files/jvm/xml02.scala
@@ -0,0 +1,55 @@
+object Test {
+def main(args:Array[String]) = {
+ import scala.xml.NodeSeq
+ import NodeSeq.view
+ import testing.UnitTest._
+
+
+ val ax = <hello foo="bar">
+ <world/>
+ </hello>
+
+ Console.println("attributes");
+
+ Console.println("one");
+ assertEquals(ax \ "@foo", "bar")
+ Console.println("two");
+ assertEquals(ax \ "@foo", xml.Text("bar"))
+
+ val bx = <hello foo="bar&amp;x"></hello>
+
+ Console.println("three");
+ assertEquals(bx \ "@foo", "bar&x")
+ Console.println("four");
+ assertSameElements(bx \ "@foo", List(xml.Text("bar"),xml.EntityRef("amp"),xml.Text("x")))
+
+ Console.println("five");
+ assertEquals(bx.toString, "<hello foo=\"bar&amp;x\"></hello>")
+
+
+ /* patterns */
+ Console.println("patterns");
+ assertEquals(<hello/> match { case <hello/> => true; case _ => false; },
+ true);
+
+
+
+ /*
+ assertEquals(ax match { case x @ <hello>
+ <world/>
+ </hello> if x \ "@foo" == "bar" => true;
+ case _ => false; },
+ true);
+
+ assertEquals(
+ <hello foo="bar">
+ crazy text world
+ </hello> match { case <hello>
+ crazy text world
+ </hello> => true;
+ case _ => false; },
+ true);
+ */
+}
+
+}
diff --git a/test/files/jvm/xml03syntax.check b/test/files/jvm/xml03syntax.check
new file mode 100644
index 0000000000..b81c53e9b1
--- /dev/null
+++ b/test/files/jvm/xml03syntax.check
@@ -0,0 +1,24 @@
+<hello>()</hello>
+passed ok
+<hello>()</hello>
+passed ok
+<hello>world</hello>
+passed ok
+<hello>1.5</hello>
+passed ok
+<hello>5</hello>
+passed ok
+<hello>true</hello>
+passed ok
+<hello>5</hello>
+passed ok
+<hello>27</hello>
+passed ok
+<hello>1 2 3 4</hello>
+1
+2
+3
+4
+<hello>2 4</hello>
+2
+4
diff --git a/test/files/jvm/xml03syntax.scala b/test/files/jvm/xml03syntax.scala
new file mode 100644
index 0000000000..7ae6473302
--- /dev/null
+++ b/test/files/jvm/xml03syntax.scala
@@ -0,0 +1,62 @@
+object Test {
+
+ import scala.testing.UnitTest._
+ import scala.xml._
+
+ def handle[A](x:Node): A = {
+ Console println x
+ x.child(0).asInstanceOf[Atom[A]].data
+ }
+
+ def main(args:Array[String]) = {
+ import scala.xml.NodeSeq
+ import NodeSeq.view
+ import testing.UnitTest._
+
+ val x0 = <hello>{}</hello>
+ val x00 = <hello>{ }</hello>
+ val xa = <hello>{ "world" }</hello>
+
+
+ assertEquals( handle[Unit](x0), {} )
+ assertEquals( handle[Unit](x00), {} )
+ assertEquals( handle[String](xa),"world" )
+
+ val xb = <hello>{ 1.5 }</hello>
+
+ assertEquals( handle[Double](xb), 1.5 )
+
+ val xc = <hello>{ 5 }</hello>
+
+ assertEquals( handle[Int](xc), 5 )
+
+ val xd = <hello>{ true }</hello>
+
+ assertEquals( handle[Boolean](xd), true )
+
+ val xe = <hello>{ 5:short }</hello>
+
+ assertEquals( handle[Short](xe), 5:short )
+
+ val xf = <hello>{ val x = 27; x }</hello>
+
+ assertEquals( handle[Int](xf), 27 )
+
+ val xg = <hello>{ List(1,2,3,4) }</hello>
+
+ Console println xg
+ for(val z <- xg.child) {
+ Console println z.toString() + {if (z.isInstanceOf[Text]) "(is text node ' ')" else ""}
+ }
+
+ val xh = <hello>{ for(val x <- List(1,2,3,4); x % 2 == 0) yield x }</hello>
+
+ Console println xh
+ for(val z <- xh.child) {
+ Console println z.toString() + {if (z.isInstanceOf[Text]) "(is text node ' ')" else ""}
+ }
+
+
+}
+
+}
diff --git a/test/files/jvm/xml04embed.check b/test/files/jvm/xml04embed.check
new file mode 100644
index 0000000000..9c9ba2b8d5
--- /dev/null
+++ b/test/files/jvm/xml04embed.check
@@ -0,0 +1,3 @@
+passed ok
+passed ok
+passed ok
diff --git a/test/files/jvm/xml04embed.scala b/test/files/jvm/xml04embed.scala
new file mode 100644
index 0000000000..807610a3b1
--- /dev/null
+++ b/test/files/jvm/xml04embed.scala
@@ -0,0 +1,17 @@
+object Test {
+
+ import scala.testing.UnitTest._
+ import scala.xml._
+
+ def main(args:Array[String]) = {
+ val ya = <x>{{</x>
+ assertEquals(ya.text, "{");
+
+ val ua = <x>}}</x>
+ assertEquals(ua.text, "}");
+
+ val za = <x>{{}}{{}}{{}}</x>
+ assertEquals(za.text, "{}{}{}");
+
+ }
+}