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>t010lexer</title>
      6 
      7 <!-- ANTLR includes -->
      8 <script type="text/javascript" src="../../lib/antlr3-all.js"></script>
      9 <script type="text/javascript" src="t010lexer.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, t010lexer, {
     20         reportError: function(re) {
     21             /* don't recover, just crash */
     22             throw re;
     23         }
     24     });
     25 
     26     function testValid() {
     27         var stream = new org.antlr.runtime.ANTLRStringStream("foobar _Ab98 \n A12sdf"),
     28 
     29             lexer = new TLexer(stream),
     30             token;
     31 
     32         token = lexer.nextToken();
     33         assertEquals(token.getType(), lexer.IDENTIFIER);
     34         assertEquals(token.getStartIndex(), 0);
     35         assertEquals(token.getStopIndex(), 5);
     36         assertEquals(token.getText(), "foobar");
     37 
     38         token = lexer.nextToken();
     39         assertEquals(token.getType(), lexer.WS);
     40         assertEquals(token.getStartIndex(), 6);
     41         assertEquals(token.getStopIndex(), 6);
     42         assertEquals(token.getText(), ' ');
     43 
     44         token = lexer.nextToken();
     45         assertEquals(token.getType(), lexer.IDENTIFIER);
     46         assertEquals(token.getStartIndex(), 7);
     47         assertEquals(token.getStopIndex(), 11);
     48         assertEquals(token.getText(), '_Ab98');
     49 
     50         token = lexer.nextToken();
     51         assertEquals(token.getType(), lexer.WS);
     52         assertEquals(token.getStartIndex(), 12);
     53         assertEquals(token.getStopIndex(), 14);
     54         assertEquals(token.getText(), " \n ");
     55 
     56         token = lexer.nextToken();
     57         assertEquals(token.getType(), lexer.IDENTIFIER);
     58         assertEquals(token.getStartIndex(), 15);
     59         assertEquals(token.getStopIndex(), 20);
     60         assertEquals(token.getText(), 'A12sdf');
     61 
     62         token = lexer.nextToken();
     63         assertEquals(token.getType(), lexer.EOF);
     64     }
     65 
     66     function testMalformedInput() {
     67         var stream = new org.antlr.runtime.ANTLRStringStream('a-b'),
     68             lexer = new TLexer(stream),
     69             token;
     70 
     71         lexer.nextToken();
     72         try {
     73             token = lexer.nextToken();
     74             fail("nextToken should have thrown error on invalid input");
     75         } catch (e) {
     76             assert(e instanceof org.antlr.runtime.NoViableAltException);
     77             assertEquals(e.getUnexpectedType(), '-');
     78             assertEquals(e.charPositionInLine, 1);
     79             assertEquals(e.line, 1);
     80         }
     81     }
     82 </script>
     83 
     84 </head>
     85 <body>
     86     <h1>t010lexer</h1>
     87 </body>
     88