aboutsummaryrefslogtreecommitdiff
path: root/antlr-3.4/runtime/JavaScript/tests/functional/t004lexer.html
blob: 14e0e1af137b3080787cb8a01c1e4d47152a39b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>t004lexer</title>

<!-- ANTLR includes -->
<script type="text/javascript" src="../../lib/antlr3-all.js"></script>
<script type="text/javascript" src="t004lexer.js"></script>

<!-- JsUnit include -->
<script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>

<!-- Test Code -->
<script type="text/javascript">
    function TLexer() {
        TLexer.superclass.constructor.apply(this, arguments);
    }
    org.antlr.lang.extend(TLexer, t004lexer, {
        reportError: function(re) {
            /* don't recover, just crash */
            throw re;
        }
    });

    function testValid() {
        var stream = new org.antlr.runtime.ANTLRStringStream("ffofoofooo"),
            lexer = new TLexer(stream),
            token;

        token = lexer.nextToken();
        assertEquals(token.getType(), lexer.FOO);
        assertEquals(token.getStartIndex(), 0);
        assertEquals(token.getStopIndex(), 0);
        assertEquals(token.getText(), "f");

        token = lexer.nextToken();
        assertEquals(token.getType(), lexer.FOO);
        assertEquals(token.getStartIndex(), 1);
        assertEquals(token.getStopIndex(), 2);
        assertEquals(token.getText(), 'fo');

        token = lexer.nextToken();
        assertEquals(token.getType(), lexer.FOO);
        assertEquals(token.getStartIndex(), 3);
        assertEquals(token.getStopIndex(), 5);
        assertEquals(token.getText(), 'foo');

        token = lexer.nextToken();
        assertEquals(token.getType(), lexer.FOO);
        assertEquals(token.getStartIndex(), 6);
        assertEquals(token.getStopIndex(), 9);
        assertEquals(token.getText(), 'fooo');

        token = lexer.nextToken();
        assertEquals(token.getType(), lexer.EOF);
    }

    function testMalformedInput() {
        var stream = new org.antlr.runtime.ANTLRStringStream('2'),
            lexer = new TLexer(stream),
            token;

        try {
            token = lexer.nextToken();
            fail("nextToken should have thrown error on invalid input");
        } catch (e) {
            assertEquals(e.expecting, 'f');
            assertEquals(e.getUnexpectedType(), '2');
        }
    }
</script>

</head>
<body>
    <h1>t004lexer</h1>
</body>
</html>