Home | History | Annotate | Download | only in functional
      1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      3 <head>
      4 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      5 <title>t012lexerXML</title>
      6 
      7 <!-- ANTLR includes -->
      8 <script type="text/javascript" src="../../lib/antlr3-all.js"></script>
      9 <script type="text/javascript" src="t012lexerXML.js"></script>
     10 
     11 <!-- JsUnit include -->
     12 <script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>
     13 
     14 <!-- Test Code -->
     15 <script type="text/javascript">
     16     function TLexer() {
     17         TLexer.superclass.constructor.apply(this, arguments);
     18     }
     19     org.antlr.lang.extend(TLexer, t012lexerXML, {
     20         reportError: function(re) {
     21             /* don't recover, just crash */
     22             throw re;
     23         }
     24     });
     25 
     26     function testValid() {
     27         var xinput = [
     28                 "<?xml version='1.0'?>",
     29                 "<!DOCTYPE component [",
     30                 "<!ELEMENT component (PCDATA|sub)*>",
     31                 "<!ATTLIST component",
     32                 "          attr CDATA #IMPLIED",
     33                 "          attr2 CDATA #IMPLIED",
     34                 ">",
     35                 "<!ELMENT sub EMPTY>",
     36                 "",
     37                 "]>",
     38                 "<component attr=\"val'ue\" attr2='val\"ue'>",
     39                 "<!-- This is a comment -->",
     40                 "Text",
     41                 "<![CDATA[huhu]]>",
     42                 "",
     43                 "&amp;",
     44                 "&lt;",
     45                 "<?xtal cursor='11'?>",
     46                 "<sub/>",
     47                 "<sub></sub>",
     48                 "</component>"
     49             ].join("\n"),
     50             xoutput = [
     51                 "XML declaration",
     52                 "Attr: version='1.0'",
     53                 "ROOTELEMENT: component",
     54                 "INTERNAL DTD: [",
     55                 "<!ELEMENT component (PCDATA|sub)*>",
     56                 "<!ATTLIST component",
     57                 "          attr CDATA #IMPLIED",
     58                 "          attr2 CDATA #IMPLIED",
     59                 ">",
     60                 "<!ELMENT sub EMPTY>",
     61                 "",
     62                 "]",
     63                 "Start Tag: component",
     64                 "Attr: attr=\"val'ue\"",
     65                 "Attr: attr2='val\"ue'",
     66                 "PCDATA: \"",
     67                 "\"",
     68                 "Comment: \"<!-- This is a comment -->\"",
     69                 "PCDATA: \"",
     70                 "Text",
     71                 "\"",
     72                 "CDATA: \"<![CDATA[huhu]]>\"",
     73                 "PCDATA: \"",
     74                 "",
     75                 "&amp;",
     76                 "&lt;",
     77                 "\"",
     78                 "PI: xtal",
     79                 "Attr: cursor='11'",
     80                 "PCDATA: \"",
     81                 "\"",
     82                 "Empty Element: sub",
     83                 "PCDATA: \"",
     84                 "\"",
     85                 "Start Tag: sub",
     86                 "End Tag: sub",
     87                 "PCDATA: \"",
     88                 "\"",
     89                 "End Tag: component"
     90             ].join("\n"),
     91             stream = new org.antlr.runtime.ANTLRStringStream(xinput),
     92             lexer = new TLexer(stream),
     93             token;
     94 
     95         while (true) {
     96             token = lexer.nextToken();
     97             if (token.type == org.antlr.runtime.Token.EOF) {
     98                 break;
     99             }
    100         }
    101 
    102         assertEquals(xoutput, lexer.lout.join("\n"));
    103     }
    104 
    105     function testMalformedInput1() {
    106         var input = [
    107             "<?xml version='1.0'?>",
    108             "<document d>",
    109             "</document>"
    110         ].join("\n"),
    111         stream = new org.antlr.runtime.ANTLRStringStream(input),
    112         lexer = new TLexer(stream),
    113         token;
    114 
    115         try {
    116             while (true) {
    117                 token = lexer.nextToken();
    118                 if (token.type == org.antlr.runtime.Token.EOF) {
    119                     break;
    120                 }
    121             }
    122             fail("block should have thrown an nvae");
    123         } catch(exc) {
    124             assert(exc instanceof org.antlr.runtime.NoViableAltException);
    125             assertEquals(exc.getUnexpectedType(), ">");
    126             assertEquals(exc.charPositionInLine, 11);
    127             assertEquals(exc.line, 2);
    128         }
    129     }
    130 
    131     function testMalformedInput2() {
    132         var input = [
    133             "<?tml version='1.0'?>",
    134             "<document>",
    135             "</document>"
    136         ].join("\n"),
    137         stream = new org.antlr.runtime.ANTLRStringStream(input),
    138         lexer = new TLexer(stream),
    139         token;
    140 
    141         try {
    142             while (true) {
    143                 token = lexer.nextToken();
    144                 if (token.type == org.antlr.runtime.Token.EOF) {
    145                     break;
    146                 }
    147             }
    148             fail("block should have thrown an nvae");
    149         } catch(exc) {
    150             assert(exc instanceof org.antlr.runtime.MismatchedSetException);
    151             assertEquals(exc.getUnexpectedType(), "t");
    152             assertEquals(exc.charPositionInLine, 2);
    153             assertEquals(exc.line, 1);
    154         }
    155     }
    156 
    157     function testMalformedInput3() {
    158         var input = [
    159         "<?xml version='1.0'?>",
    160         "<docu ment attr=\"foo\">",
    161         "</document>"
    162         ].join("\n"),
    163         stream = new org.antlr.runtime.ANTLRStringStream(input),
    164         lexer = new TLexer(stream),
    165         token;
    166 
    167         try {
    168             while (true) {
    169                 token = lexer.nextToken();
    170                 if (token.type == org.antlr.runtime.Token.EOF) {
    171                     break;
    172                 }
    173             }
    174             fail("block should have thrown an nvae");
    175         } catch(exc) {
    176             assert(exc instanceof org.antlr.runtime.NoViableAltException);
    177             assertEquals(exc.getUnexpectedType(), "a");
    178             assertEquals(exc.charPositionInLine, 11);
    179             assertEquals(exc.line, 2);
    180         }
    181     }
    182 </script>
    183 </head>
    184 <body>
    185     <h1>t012lexerXML</h1>
    186 </body>
    187 </html>
    188