From 483b35519a4a687e052b44fd154cf47c67626650 Mon Sep 17 00:00:00 2001 From: buraq Date: Tue, 2 Mar 2004 15:48:39 +0000 Subject: xml parsing with comments --- sources/scala/tools/scalac/ast/parser/Parser.scala | 29 ++++++--- .../scala/tools/scalac/ast/parser/Scanner.scala | 71 ++++++++++++---------- 2 files changed, 58 insertions(+), 42 deletions(-) (limited to 'sources') diff --git a/sources/scala/tools/scalac/ast/parser/Parser.scala b/sources/scala/tools/scalac/ast/parser/Parser.scala index baa8ca728e..02057a6feb 100644 --- a/sources/scala/tools/scalac/ast/parser/Parser.scala +++ b/sources/scala/tools/scalac/ast/parser/Parser.scala @@ -915,8 +915,10 @@ class Parser(unit: Unit) { val pos1 = s.skipToken(); if (s.token == IDENTIFIER && s.name == Names.STAR) { s.nextToken(); + /* if( s.token != RPAREN ) syntaxError(s.pos, " escaping sequences only allowed for last argument", true); + */ t = make.Typed( pos, t, make.Ident(pos1, TypeNames.WILDCARD_STAR)); } else { @@ -992,7 +994,7 @@ class Parser(unit: Unit) { SYMBOLLIT | TRUE | FALSE | NULL => t = literal(false); case IDENTIFIER | THIS | SUPER => - t = if( s.name == LT ) { + t = if(( s.name == LT )&&( unit.global.xmlMarkup )) { xmlExprTop(); /* top-level xml expression */ } else { stableRef(true, false); @@ -1053,7 +1055,11 @@ class Parser(unit: Unit) { def xmlExprTop():Tree = { val pos = s.pos; var t = xmlExpr(); + //Console.println("old:"+token2string( s.token ) ); + s.token = EMPTY; s.nextToken(); + //Console.println("new:"+token2string( s.token ) ); + //Console.println("line:"+s.cline); if(( s.token == IDENTIFIER ) && ( s.name == LT )) { val ts = new myTreeList(); ts.append( t ); @@ -1323,7 +1329,7 @@ class Parser(unit: Unit) { case IDENTIFIER | THIS => if (s.name == BAR) { make.Sequence(s.pos, Tree.EMPTY_ARRAY); // ((nothing)) - } else if (s.name == LT) { + } else if(( s.name == LT )&&( unit.global.xmlMarkup )) { xmlPatternTop() } else { var t = stableId(); @@ -1378,7 +1384,7 @@ class Parser(unit: Unit) { endch match { case '"' | '\'' => { s.nextch(); - attrValue = s.xmlValue( endch ); + attrValue = s.xmlAttribValue( endch ); s.xmlToken( endch.asInstanceOf[char] ); s.xmlSpaceOpt(); } @@ -1420,19 +1426,23 @@ class Parser(unit: Unit) { s.ch match { case '<' => { /* tag */ s.nextch(); - if( s.ch != '/' ) { /* search end tag */ - ts.append( xmlExpr() ); - s.xmlSpaceOpt(); - } else { - exit = true + s.ch match { + case '/' => exit = true; + case '!' => + s.xmlComment(); + case _ => + /* search end tag */ + ts.append( xmlExpr() ); + s.xmlSpaceOpt(); } } + case '{' => { /* Scala block */ while( s.ch == '{' ) { s.nextToken(); s.nextToken(); val bs = new myTreeList(); - val b = block( s.pos ); + val b = expr(true,false); //block( s.pos ); if( s.token != RBRACE ) { s.xml_syntaxError(" expected end of Scala block"); } @@ -1447,7 +1457,6 @@ class Parser(unit: Unit) { } } xmlEndTag( elemName ); - s.xmlSpaceOpt(); if( attrMap.isEmpty ) makeXML( pos, elemName, ts.toArray() ); else diff --git a/sources/scala/tools/scalac/ast/parser/Scanner.scala b/sources/scala/tools/scalac/ast/parser/Scanner.scala index b52b815e9a..174040d220 100644 --- a/sources/scala/tools/scalac/ast/parser/Scanner.scala +++ b/sources/scala/tools/scalac/ast/parser/Scanner.scala @@ -663,37 +663,29 @@ class Scanner(_unit: Unit) extends TokenData { def xml_syntaxError(s:String) = { syntaxError("in XML literal: "+s); - nextch(); + xml_nextch(); } - /* this helper functions updates ccol and cline, only necessary in whitespace - * production + /* this helper functions updates ccol and cline */ def xml_nextch() = { - try { - nextch(); - } catch { - case e:ArrayIndexOutOfBoundsException => { - token = EOF; - syntaxError(lastpos, "unclosed XML literal"); - throw new ApplicationError("unclosed XML literal"); - } - } + nextch(); ch match { - case '\r' => { + case SU => syntaxError(lastpos, "unclosed XML literal"); token = EOF; + case CR => cline = cline + 1; ccol = 0; nextch(); /* in compliance with XML spec */ - if( ch == '\n' ) { + if( ch == LF ) { ccol = 0; } - } - case '\n' => { + case LF => { cline = cline + 1; ccol = 0; } case _ => } + pos = Position.encode(cline, ccol); } def xml_isSpace() = ch match { @@ -705,7 +697,6 @@ class Scanner(_unit: Unit) extends TokenData { while( xml_isSpace() ) { xml_nextch(); } - pos = Position.encode(cline, ccol); } /** [3] S ::= (#x20 | #x9 | #xD | #xA)+ @@ -715,7 +706,6 @@ class Scanner(_unit: Unit) extends TokenData { xml_nextch(); xmlSpaceOpt() } else { - pos = Position.encode(cline, ccol); xml_syntaxError("whitespace expected"); } } @@ -752,9 +742,8 @@ class Scanner(_unit: Unit) extends TokenData { if( xml_isNameStart() ) { val index = bp; while( xml_isNameChar() ) { - nextch(); + xml_nextch(); } - pos = Position.encode(cline, ccol); Name.fromAscii(buf, index, bp - index); } else { xml_syntaxError("name expected"); @@ -762,28 +751,46 @@ class Scanner(_unit: Unit) extends TokenData { } } - def xmlValue(endch:char):String = { + /* consuming everything up to the next endch */ + def xmlValue(endch:char):String = xmlValue(endch, true); + + def xmlValue(endch:char, keep:boolean):String = { lastpos = pos; val index = bp; - while ( ch != endch ) { - if(( ch == '<' )||( ch == '&' )) { - pos = Position.encode(cline, ccol); - xml_syntaxError(ch.asInstanceOf[char]+" not allowed here"); - } - xml_nextch(); - }; - pos = Position.encode(cline, ccol); - new String(buf, index, bp-index); + while ( ch != endch ) { xml_nextch();}; + pos = Position.encode( cline, ccol ); + if( keep ) + new String(buf, index, bp-index); + else + null + } + + def xmlAttribValue(endch:char):String = { + val s = xmlValue(endch, true); + if( s.indexOf('<') != -1 ) { + xml_syntaxError("'<' not allowed in attrib value"); + "--syntax error--" + } else { + s + } } def xmlText():String = xmlValue('<'); + def xmlComment() = { + xmlToken('!'); + xmlToken('-'); + xmlToken('-'); + xmlValue('-', false); + xmlToken('-'); + xmlToken('-'); + xmlToken('>'); + }; + def xmlToken(that:char):unit = { if( ch == that ) { xml_nextch(); - pos = Position.encode(cline, ccol); } else { - pos = Position.encode(cline, ccol); xml_syntaxError("'"+that+"' expected instead of '"+ch.asInstanceOf[char]+"'"); } } -- cgit v1.2.3