~ubuntu-branches/ubuntu/jaunty/alex/jaunty

« back to all changes in this revision

Viewing changes to alex/tests/tokens.x

  • Committer: Bazaar Package Importer
  • Author(s): Ian Lynagh (wibble)
  • Date: 2003-10-01 12:31:01 UTC
  • Revision ID: james.westby@ubuntu.com-20031001123101-yquo14mvjqh3e0sk
Tags: upstream-2.0
ImportĀ upstreamĀ versionĀ 2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{
 
2
module Main (main) where
 
3
import System.Exit
 
4
}
 
5
 
 
6
%wrapper "basic"
 
7
 
 
8
$digit = 0-9                    -- digits
 
9
$alpha = [a-zA-Z]               -- alphabetic characters
 
10
 
 
11
tokens :-
 
12
 
 
13
  $white+                               ;
 
14
  "--".*                                ;
 
15
  let                                   { \s -> Let }
 
16
  in                                    { \s -> In }
 
17
  $digit+                               { \s -> Int (read s) }
 
18
  [\=\+\-\*\/\(\)]                      { \s -> Sym (head s) }
 
19
  $alpha [$alpha $digit \_ \']*         { \s -> Var s }
 
20
 
 
21
{
 
22
-- Each right-hand side has type :: String -> Token
 
23
 
 
24
-- The token type:
 
25
data Token =
 
26
        Let             |
 
27
        In              |
 
28
        Sym Char        |
 
29
        Var String      |
 
30
        Int Int         |
 
31
        Err 
 
32
        deriving (Eq,Show)
 
33
 
 
34
main = if test1 /= result1 then exitFailure
 
35
                           else exitWith ExitSuccess
 
36
 
 
37
test1 = alexScanTokens "  let in 012334\n=+*foo bar__'"
 
38
result1 = [Let,In,Int 12334,Sym '=',Sym '+',Sym '*',Var "foo",Var "bar__'"]
 
39
 
 
40
}