diff options
author | buraq <buraq@epfl.ch> | 2004-07-05 15:35:58 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2004-07-05 15:35:58 +0000 |
commit | f0462d89218b6cfbd050cd1611a81507692388bf (patch) | |
tree | 022599bbe5ea2d760f12eed5c3be78fd542da57b /sources | |
parent | e667e3d3d6b2d6580a126d0cfda7e77eb31e767d (diff) | |
download | scala-f0462d89218b6cfbd050cd1611a81507692388bf.tar.gz scala-f0462d89218b6cfbd050cd1611a81507692388bf.tar.bz2 scala-f0462d89218b6cfbd050cd1611a81507692388bf.zip |
regexps refactored
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/util/regexp/Alphabet.scala | 3 | ||||
-rw-r--r-- | sources/scala/util/regexp/Base.scala | 31 | ||||
-rw-r--r-- | sources/scala/util/regexp/PointedHedgeExp.scala | 15 | ||||
-rw-r--r-- | sources/scala/util/regexp/SyntaxErrorException.scala | 6 | ||||
-rw-r--r-- | sources/scala/util/regexp/WordExp.scala | 17 | ||||
-rw-r--r-- | sources/scala/xml/dtd/Decl.scala | 4 | ||||
-rw-r--r-- | sources/scala/xml/dtd/Parser.scala | 7 | ||||
-rw-r--r-- | sources/scala/xml/dtd/Validation.scala | 4 |
8 files changed, 80 insertions, 7 deletions
diff --git a/sources/scala/util/regexp/Alphabet.scala b/sources/scala/util/regexp/Alphabet.scala new file mode 100644 index 0000000000..0ae119e8e5 --- /dev/null +++ b/sources/scala/util/regexp/Alphabet.scala @@ -0,0 +1,3 @@ +package scala.util.regexp ; + +trait Alphabet ; diff --git a/sources/scala/util/regexp/Base.scala b/sources/scala/util/regexp/Base.scala new file mode 100644 index 0000000000..9eb93f4659 --- /dev/null +++ b/sources/scala/util/regexp/Base.scala @@ -0,0 +1,31 @@ +package scala.util.regexp ; + +/** basic regular expressions */ + +trait Base { + + type regexp <: RegExp; + + abstract class RegExp; + + /** Alt( R,R,R* ) */ + case class Alt(rs: regexp*) extends RegExp { + + // check rs \in R,R,R* + + if({ var i = 0; + val it = rs.elements; + while( it.hasNext ) {it.next; i=i+1}; + i} < 2) + throw new SyntaxErrorExpression("need at least 2 branches in alt") + } + case class Sequ(rs: regexp*) extends RegExp ; + case class Star(r: regexp) extends RegExp ; + case object Eps extends RegExp { + override def toString() = "Eps"; + } + + /** this class can be used to add meta information to regexps */ + class Meta( r:regexp ) extends RegExp ; + +} diff --git a/sources/scala/util/regexp/PointedHedgeExp.scala b/sources/scala/util/regexp/PointedHedgeExp.scala new file mode 100644 index 0000000000..b3cccf2198 --- /dev/null +++ b/sources/scala/util/regexp/PointedHedgeExp.scala @@ -0,0 +1,15 @@ +package scala.util.regexp ; + +/** pointed regular hedge expressions, a useful subclass of full rhe */ +trait PointedHedgeExp[ A <: Alphabet ] extends Base { + + type label = A; + + type regexp <: RegExp; + + case class Node(label: A, r: regexp) extends RegExp ; + case class TopIter(r1: regexp, r2: regexp) extends RegExp ; + + case object Point extends RegExp ; + +} diff --git a/sources/scala/util/regexp/SyntaxErrorException.scala b/sources/scala/util/regexp/SyntaxErrorException.scala new file mode 100644 index 0000000000..e1ada9daca --- /dev/null +++ b/sources/scala/util/regexp/SyntaxErrorException.scala @@ -0,0 +1,6 @@ +package scala.util.regexp ; + +/** this runtime exception is thrown if an attempt to instantiate a + * syntactically incorrect expression is detected */ +class SyntaxErrorExpression(e: String) + extends java.lang.RuntimeException(e); diff --git a/sources/scala/util/regexp/WordExp.scala b/sources/scala/util/regexp/WordExp.scala new file mode 100644 index 0000000000..1151b8b11f --- /dev/null +++ b/sources/scala/util/regexp/WordExp.scala @@ -0,0 +1,17 @@ +package scala.util.regexp ; + +/** regular word expressions. use them with an alphabet: <pre> +abstract class IntLabels extends Alphabet; +object IntWordExp extends WordExp[IntLabels] { + type regexp = RegExp; +}; + * </pre> + */ + +trait WordExp[ A <: Alphabet ] extends Base { + + type label = A; + + case class Letter(a: label) extends RegExp; + +} diff --git a/sources/scala/xml/dtd/Decl.scala b/sources/scala/xml/dtd/Decl.scala index dd0a71f435..246ee4a5a0 100644 --- a/sources/scala/xml/dtd/Decl.scala +++ b/sources/scala/xml/dtd/Decl.scala @@ -21,9 +21,9 @@ case class ElemDecl( name:String , attribs:Map[String,AttrDecl] ) extends MarkupDecl { - final val parsedContentModel:RegExp = { + final val parsedContentModel:ContentModel.RegExp = { try { - RegExp.parse( contentModel ); + ContentModel.parse( contentModel ); } catch { case _:Error => Console.println( "error parsing declaration of " + name ); diff --git a/sources/scala/xml/dtd/Parser.scala b/sources/scala/xml/dtd/Parser.scala index b0e9e369ec..410e82ce06 100644 --- a/sources/scala/xml/dtd/Parser.scala +++ b/sources/scala/xml/dtd/Parser.scala @@ -3,6 +3,7 @@ package scala.xml.dtd ; /** Parser for regexps (content models in DTD element declarations) */ object Parser with Scanner { // a bit too permissive concerning #PCDATA + import ContentModel._ ; /** parses the argument to a regexp */ def parse(s:String):RegExp = { initScanner( s ); contentspec } @@ -61,7 +62,7 @@ object Parser with Scanner { // a bit too permissive concerning #PCDATA PCDATA_ else { val t = choiceRest( PCDATA_ ); - if( !t.mixed ) + if( !isMixed( t ) ) error("mixed content models must be like (#PCDATA.|.|.|.)*"); accept( RPAREN ); // lax: (workaround for buggy Java XML parser in JDK1.4.2) @@ -115,14 +116,14 @@ object Parser with Scanner { // a bit too permissive concerning #PCDATA //Console.println("particle, token="+token2string(token)); token match { case LPAREN => nextToken; sOpt; regexp; - case NAME => val a = RNode( value ); nextToken; maybeSuffix( a ) + case NAME => val a = Letter(ElemName(value)); nextToken; maybeSuffix( a ) case _ => error("expected '(' or Name, got:"+token2string( token )); } } // atom ::= name def atom = token match { - case NAME => val a = RNode( value ); nextToken; a + case NAME => val a = Letter(ElemName(value)); nextToken; a case _ => error("expected Name, got:"+token2string( token )); } } diff --git a/sources/scala/xml/dtd/Validation.scala b/sources/scala/xml/dtd/Validation.scala index 6073b5aaa8..0055b0844c 100644 --- a/sources/scala/xml/dtd/Validation.scala +++ b/sources/scala/xml/dtd/Validation.scala @@ -3,7 +3,7 @@ package scala.xml.dtd ; import scala.collection.mutable; import scala.collection.Set; import scala.collection.Map; - +import ContentModel._ ; class ElementValidator(namespace$$: String, elemSpec: RegExp) { final val namespace = namespace$$.intern(); @@ -56,7 +56,7 @@ class ElementValidator(namespace$$: String, elemSpec: RegExp) { case Star(z:Alt) if(( z.rs.length == 1 )&& (z.rs(0) match { case PCDATA_ => true case _ => false - })) => new MixedModeAutom( elemSpec.getLabels ); + })) => new MixedModeAutom(getLabels( elemSpec )); case _ => TrivialAutom; } def validate( nodes:Seq[Node] ):Unit = autom.run( nodes ); |