diff options
author | michelou <michelou@epfl.ch> | 2005-05-20 09:36:56 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2005-05-20 09:36:56 +0000 |
commit | 535c7e54fcdb3950ae490cd40ad5ef9da1882d98 (patch) | |
tree | c8eb1912814a28b7c3d48f3280aa9ec22cc7f2fa | |
parent | e215fbc8cf0305ba7702db9810a456f162f80326 (diff) | |
download | scala-535c7e54fcdb3950ae490cd40ad5ef9da1882d98.tar.gz scala-535c7e54fcdb3950ae490cd40ad5ef9da1882d98.tar.bz2 scala-535c7e54fcdb3950ae490cd40ad5ef9da1882d98.zip |
- cleaned up.
-rw-r--r-- | sources/scala/tools/scalac/ast/parser/MarkupParser.scala | 85 | ||||
-rw-r--r-- | sources/scala/xml/parsing/TokenTests.scala | 65 |
2 files changed, 82 insertions, 68 deletions
diff --git a/sources/scala/tools/scalac/ast/parser/MarkupParser.scala b/sources/scala/tools/scalac/ast/parser/MarkupParser.scala index 75d30bdfcd..a817767544 100644 --- a/sources/scala/tools/scalac/ast/parser/MarkupParser.scala +++ b/sources/scala/tools/scalac/ast/parser/MarkupParser.scala @@ -1,34 +1,38 @@ -/* ____ ____ ____ ____ ______ *\ -** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** -** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002-2004, LAMP/EPFL ** -** /_____/\____/\___/\____/____/ ** +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ \* */ -// $Id$ +import java.lang.{Integer, Long, Float, Double}; +import scalac._; import scalac.ast._; import scalac.atree.AConstant; -import scalac._; import scalac.symtab.Modifiers; -import scala.tools.util.Position; -import java.lang.{Integer, Long, Float, Double}; + import scala.Iterator; -import scala.tools.scalac.util.NewArray; import scala.collection.immutable.ListMap ; import scala.collection.mutable; +import scala.tools.scalac.util.NewArray; +import scala.tools.util.Position; import scala.xml.{Text,TextBuffer}; package scala.tools.scalac.ast.parser { -class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean ) /*with scala.xml.parsing.MarkupParser[Tree,Tree] */{ +class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean) /*with scala.xml.parsing.MarkupParser[Tree,Tree] */{ - import Tokens.{EMPTY, LBRACE, RBRACE} ; + import Tokens.{EMPTY, LBRACE, RBRACE}; import scala.tools.scalac.ast.{TreeList => myTreeList} final val preserveWS = presWS; /** the XML tree factory */ - val handle: SymbolicXMLBuilder = new SymbolicXMLBuilder( unit.global.make, unit.global.treeGen, p, presWS ); + val handle: SymbolicXMLBuilder = + new SymbolicXMLBuilder(unit.global.make, unit.global.treeGen, p, presWS); /** holds the position in the source file */ /*[Duplicate]*/ var pos: Int = _; @@ -74,7 +78,7 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean */ /*[Duplicate]*/ def xAttributes = { var aMap = new mutable.HashMap[String, Tree](); - while( xml.Parsing.isNameStart( ch )) { + while (xml.Parsing.isNameStart(ch)) { val key = xName; xEQ; val delim = ch; @@ -105,16 +109,16 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean /** attribute value, terminated by either ' or ". value may not contain <. * @param endch either ' or " */ - /*[Duplicate]*/ def xAttributeValue( endch:char ):String = { - while ( ch != endch ) { - putChar( ch ); + /*[Duplicate]*/ def xAttributeValue(endCh: char): String = { + while (ch != endCh) { + putChar(ch); nextch; }; val str = cbuf.toString(); - cbuf.setLength( 0 ); + cbuf.setLength(0); // @todo: normalize attribute value // well-formedness constraint - if( str.indexOf('<') != -1 ) { + if (str.indexOf('<') != -1) { reportSyntaxError( "'<' not allowed in attrib value" ); "" } else { str @@ -128,7 +132,7 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean /*[Duplicate]*/ def xTag: Pair[String, mutable.Map[String, Tree]] = { val elemName = xName; xSpaceOpt; - val aMap = if(xml.Parsing.isNameStart( ch )) { + val aMap = if (xml.Parsing.isNameStart(ch)) { xAttributes; } else { new mutable.HashMap[String, Tree](); @@ -160,7 +164,7 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean val pos1 = pos; val sb:StringBuffer = new StringBuffer(); while (true) { - if( ch==']' && + if (ch==']' && { sb.append( ch ); nextch; ch == ']' } && { sb.append( ch ); nextch; ch == '>' } ) { sb.setLength( sb.length() - 2 ); @@ -181,7 +185,7 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean val hex = ( ch == 'x' ) && { nextch; true }; val base = if (hex) 16 else 10; var i = 0; - while( ch != ';' ) { + while (ch != ';') { ch match { case '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => i = i * base + Character.digit( ch, base ); @@ -293,11 +297,12 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean val pos1 = pos; val Tuple2(qname, attrMap) = xTag; //Console.println("MarkupParser::element("+qname+","+attrMap+")"); - if(ch == '/') { // empty element + if (ch == '/') { // empty element xToken('/'); xToken('>'); handle.element( pos1, qname, attrMap, new mutable.ListBuffer[Tree] ); - } else { // handle content + } + else { // handle content xToken('>'); debugLastStartElement.push(Pair(pos1,qname)); val ts = content; @@ -336,9 +341,10 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean /** scan [3] S ::= (#x20 | #x9 | #xD | #xA)+ */ /*[Duplicate]*/ def xSpace = { - if( xml.Parsing.isSpace( ch ) ) { + if (xml.Parsing.isSpace(ch)) { nextch; xSpaceOpt - } else { + } + else { reportSyntaxError("whitespace expected"); } } @@ -413,7 +419,7 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean var tree = element; xSpaceOpt; // parse more XML ? - if( ch=='<' ) { + if (ch == '<') { val ts = new mutable.ArrayBuffer[Tree](); ts.append( tree ); while( ch == '<' ) { @@ -426,7 +432,8 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean //Console.println("out of xLiteral, parsed:"+tree.toString()); s.xSync2; tree - } catch { + } + catch { case _:ArrayIndexOutOfBoundsException => s.syntaxError(debugLastStartElement.top._1, "missing end tag in XML literal for <" @@ -444,9 +451,9 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean handle.isPattern = true; val pos = s.pos; var tree = xPattern; xSpaceOpt; - if( ch == '<' ) { + if (ch == '<') { val ts = new myTreeList(); ts.append( tree ); - while( ch == '<' && lookahead != '-' ) { + while (ch == '<' && lookahead != '-') { nextch; ts.append( xPattern ); xSpaceOpt; @@ -477,10 +484,10 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean /** xScalaPatterns ::= patterns */ - def xScalaPatterns:Array[Tree] = { + def xScalaPatterns: Array[Tree] = { sync; val b = p.patterns(); - if( s.token != RBRACE ) + if (s.token != RBRACE) reportSyntaxError(" expected end of Scala patterns"); init; return b @@ -506,11 +513,11 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean * | `{` scalablock `}` def xAttributes = { var aMap = new mutable.HashMap[String, AttribValue[Tree]]; - while( xml.Parsing.isNameStart( ch )) { + while (xml.Parsing.isNameStart(ch)) { val key = xName; xEQ; val delim = ch; - val value:Tree = ch match { + val value: Tree = ch match { case '"' | '\'' => val pos1 = pos; nextch; @@ -536,8 +543,8 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean */ - def reportSyntaxError(str:String) = { - s.syntaxError("in XML literal: "+str); + def reportSyntaxError(str: String) = { + s.syntaxError("in XML literal: " + str); nextch; } @@ -565,7 +572,7 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean xToken('>'); var ts = new mutable.ArrayBuffer[Tree]; var exit = false; - while( !exit ) { + while (! exit) { val pos2 = pos; if( xEmbeddedBlock ) { ts ++ xScalaPatterns; @@ -585,16 +592,16 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean ts ++ xScalaPatterns; } // postcond: xEmbeddedBlock = false; - if( xEmbeddedBlock ) throw new ApplicationError(); // assert + if (xEmbeddedBlock) throw new ApplicationError(); // assert case _ => // teMaxt appendText( pos2, ts, xText ); // here xEmbeddedBlock might be true; //if( xEmbeddedBlock ) throw new ApplicationError("after:"+text); // assert } } - xEndTag( qname ); + xEndTag(qname); debugLastStartElement.pop; - handle.makeXMLpat( pos1, qname, ts ); + handle.makeXMLpat(pos1, qname, ts); } } /* class MarkupParser */ diff --git a/sources/scala/xml/parsing/TokenTests.scala b/sources/scala/xml/parsing/TokenTests.scala index d06660303d..c9eb2235c3 100644 --- a/sources/scala/xml/parsing/TokenTests.scala +++ b/sources/scala/xml/parsing/TokenTests.scala @@ -1,14 +1,16 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2003-2005, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** ** $Id$ \* */ -package scala.xml.parsing ; -/** helper functions for parsing XML fragments +package scala.xml.parsing; + +/** + * Helper functions for parsing XML fragments */ trait TokenTests { @@ -19,7 +21,7 @@ trait TokenTests { } /** (#x20 | #x9 | #xD | #xA)+ */ - final def isSpace( cs:Seq[Char] ):Boolean = { + final def isSpace(cs: Seq[Char]): Boolean = { val it = cs.elements; it.hasNext && it.forall { isSpace }; } @@ -29,7 +31,7 @@ trait TokenTests { * * see [4] and Appendix B of XML 1.0 specification */ - def isNameChar( ch:Char ) = isNameStart( ch ) || (ch match { + def isNameChar(ch: Char) = isNameStart(ch) || (ch match { case '.' | '-' | ':' => true; case _ => java.lang.Character.getType( ch ).asInstanceOf[Byte] match { case java.lang.Character.COMBINING_SPACING_MARK => true; // Mc @@ -48,13 +50,13 @@ trait TokenTests { * We do not allow a name to start with ':'. * see [3] and Appendix B of XML 1.0 specification */ - def isNameStart( ch:Char ) = - java.lang.Character.getType( ch ).asInstanceOf[Byte] match { - case java.lang.Character.LOWERCASE_LETTER => true; - case java.lang.Character.UPPERCASE_LETTER => true; - case java.lang.Character.OTHER_LETTER => true; - case java.lang.Character.TITLECASE_LETTER => true; - case java.lang.Character.LETTER_NUMBER => true; + def isNameStart(ch: Char) = + java.lang.Character.getType(ch).asInstanceOf[Byte] match { + case java.lang.Character.LOWERCASE_LETTER => true; + case java.lang.Character.UPPERCASE_LETTER => true; + case java.lang.Character.OTHER_LETTER => true; + case java.lang.Character.TITLECASE_LETTER => true; + case java.lang.Character.LETTER_NUMBER => true; case _ => ch match { case '_' => true case _ => false; @@ -65,26 +67,29 @@ trait TokenTests { * * see [5] of XML 1.0 specification */ - def isName( s:String ):boolean = { + def isName(s: String): boolean = { if( s.length() > 0 ) { val z:Seq[Char] = s; val y = z.elements; - if( isNameStart( y.next ) ) { - while( y.hasNext && isNameChar( y.next ) ) {}; + if (isNameStart(y.next)) { + while (y.hasNext && isNameChar(y.next)) {}; !y.hasNext } else false; } else false; } - def isPubIDChar( c:Char ) = { - //Console.println("char: '"+c+"'"); - c match { + def isPubIDChar(ch: Char): boolean = { + //Console.println("char: '" + ch + "'"); + ch match { case '\u0020' | '\u000D' | '\u000A' => true; case _ if - ('0' <= c && c <= '9')||('a' <= c && c <= 'z')||('A' <= c && c <= 'Z') => true; - case '-' | '\''| '(' | ')' | '+' | ',' | '.' | '/' | ':' | '=' | - '?' | ';' | '!' | '*' | '#' | '@' | '$' | '_' | '%' => true - case _ => //Console.println("false: '"+c+"'"); + ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'z') || + ('A' <= ch && ch <= 'Z') => true; + case '-' | '\''| '(' | ')' | '+' | ',' | '.' | + '/' | ':' | '=' | '?' | ';' | '!' | '*' | + '#' | '@' | '$' | '_' | '%' => true + case _ => + //Console.println("false: '" + ch + "'"); false; } } @@ -99,12 +104,12 @@ trait TokenTests { */ def isValidIANAEncoding(ianaEncoding: Seq[Char]): Boolean = { val it = ianaEncoding.elements; - if(!it.hasNext) + if (!it.hasNext) return false; var c = it.next; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { - while(it.hasNext) { + while (it.hasNext) { c = it.next; if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '.' && c != '_' && @@ -121,18 +126,20 @@ trait TokenTests { s.indexOf('"') == -1 || s.indexOf('\'') == -1 } - def checkPubID( s:String ): Boolean = { + def checkPubID(s: String): Boolean = { //Console.println("checkPubID of \""+s+"\""); - if( s.length() > 0 ) { - val z:Seq[Char] = s; + if (s.length() > 0) { + val z: Seq[Char] = s; val y = z.elements; var c = ' '; - while( y.hasNext && isPubIDChar( c ) ){ + while (y.hasNext && isPubIDChar(c)) { //Console.println(c); c = y.next }; !y.hasNext - } else true + } + else + true } } |