File: unicode.x

package info (click to toggle)
alex 3.4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 768 kB
  • sloc: haskell: 4,648; makefile: 138; yacc: 56; ansic: 4
file content (87 lines) | stat: -rw-r--r-- 1,573 bytes parent folder | download | duplicates (4)
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
79
80
81
82
83
84
85
86
87
{
-- Tests the basic operation.
module Main where

import Data.Char (toUpper)
import Control.Monad
import System.Exit
import System.IO
}

%wrapper "monad"

@word = [A-Za-z]+

tokens :-

<0> {
   "αω"        { string }
   [AΓ]        { character }
   .           { other }
}


{
string :: AlexInput -> Int -> Alex String
string (_,_,_,_) _ = return "string!"

other :: AlexInput -> Int -> Alex String
other (_,_,_,input) len = return (take len input)

character :: AlexInput -> Int -> Alex String
character (_,_,_,_) _ = return "PING!"

alexEOF :: Alex String
alexEOF = return "stopped."

scanner :: String -> Either String [String]
scanner str = runAlex str $ do
  let loop = do tok <- alexMonadScan
		if tok == "stopped." || tok == "error."
			then return [tok]
			else do toks <- loop
				return (tok:toks)
  loop

main :: IO ()
main = do
  let test1 = scanner str1
  when (test1 /= out1) $
	do hPutStrLn stderr "Test 1 failed:"
	   print test1
	   exitFailure

  let test2 = scanner str2
  when (test2 /= out2) $
	do hPutStrLn stderr "Test 2 failed:"
	   print test2
	   exitFailure

  let test3 = scanner str3
  when (test3 /= out3) $
	do hPutStrLn stderr "Test 3 failed:"
	   print test3
	   exitFailure

  let test4 = scanner str4
  when (test4 /= out4) $
	do hPutStrLn stderr "Test 4 failed:"
	   print test4
	   exitFailure



str1 = "A."
out1 = Right ["PING!",".","stopped."]

str2 = "\n"
out2 = Left "lexical error at line 1, column 1"


str3 = "αω --"
out3 = Right ["string!"," ","-","-","stopped."]

str4 = "βΓ"
out4 = Right ["β","PING!","stopped."]

}