[go: up one dir, main page]

File: Tokens_gscan.x

package info (click to toggle)
alex 3.2.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 924 kB
  • sloc: haskell: 4,141; xml: 1,583; makefile: 123; yacc: 56; ansic: 4
file content (40 lines) | stat: -rw-r--r-- 794 bytes parent folder | download | duplicates (11)
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
{
module Main (main) where
}

%wrapper "gscan"

$digit = 0-9			-- digits
$alpha = [a-zA-Z]		-- alphabetic characters

tokens :-

  $white+				;
  "--".*				;
  let					{ tok (\p s -> Let p) }
  in					{ tok (\p s -> In p) }
  $digit+				{ tok (\p s -> Int p (read s)) }
  [\=\+\-\*\/\(\)]			{ tok (\p s -> Sym p (head s)) }
  $alpha [$alpha $digit \_ \']*		{ tok (\p s -> Var p s) }

{
-- Some action helpers:
tok f p c str len cont (sc,state) = f p (take len str) : cont (sc,state)

-- The token type:
data Token =
	Let AlexPosn		|
	In  AlexPosn		|
	Sym AlexPosn Char	|
	Var AlexPosn String	|
	Int AlexPosn Int	|
	Err AlexPosn
	deriving (Eq,Show)

main = do
  s <- getContents
  print (alexGScan stop undefined s)
  where
	stop p c "" (sc,s) = []
	stop p c _  (sc,s) = error "lexical error"
}