~ubuntu-branches/ubuntu/utopic/haskell-uulib/utopic

« back to all changes in this revision

Viewing changes to examples/parser/Scanner.x

  • Committer: Bazaar Package Importer
  • Author(s): Marco Túlio Gontijo e Silva
  • Date: 2009-04-08 20:01:10 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090408200110-96hu9fr918e1wsr1
Tags: 0.9.10-0.1
* Non-maintainer upload.
* New upstream version.  Closes: #523214.
* Use new version of haskell-devscripts.
* debian/control:
  - Use new Standards-Version.
  - cpphs is not Indep.
* debian/patches: Remove directory.
* debian/haskell-uulib-doc.doc-base: haddock's file are stored now in
  /usr/share/libghc6-uulib-doc/html/.
* debian/haskell-uulib-doc.examples: Create file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{
 
2
-- alex scanner for use with uulib
 
3
--   compile with alex -o Scanner.hs -g Scanner.x
 
4
module Scanner(tokenize) where
 
5
 
 
6
import UU.Scanner
 
7
}
 
8
 
 
9
$litChar   = [^[\" \\]]
 
10
$identChar = [a-zA-Z0-9\'_]
 
11
 
 
12
 
 
13
tokens :-
 
14
  $white+                                      ;                               -- whitespace
 
15
  "--".*                                       ;                               -- comment
 
16
 
 
17
  \" ($litChar | \\ \\ | \\ \" )* \"           { valueToken TkString }         -- string
 
18
  [0-9]+                                       { valueToken TkInteger16 }      -- int
 
19
  
 
20
  ( let | in )                                 { reserved }                    -- reserved keywords
 
21
  [\(\)\=]                                     { reserved }                    -- reserved symbols
 
22
  [\+\*]                                       { reserved }                    -- operators
 
23
 
 
24
  [a-zA-Z] $identChar*                         { valueToken TkVarid }          -- identifier
 
25
 
 
26
 
 
27
{
 
28
-- boilerplate code needed for Alex
 
29
type AlexInput = (Pos, String)
 
30
 
 
31
alexInputPrevChar :: AlexInput -> Char
 
32
alexInputPrevChar = error "alexInputPrevChar: there is no need to go back in the input."
 
33
 
 
34
alexGetChar :: AlexInput -> Maybe (Char, AlexInput)
 
35
alexGetChar (_, []) = Nothing
 
36
alexGetChar (p, (c:cs))
 
37
  = let p' = adv p c
 
38
    in Just (c, (p', cs))
 
39
 
 
40
-- use the Alex scanner to generate a list of tokens for the uulib token parsers
 
41
tokenize :: String -> String -> [Token]
 
42
tokenize filename str
 
43
  = go (initpos, str)
 
44
  where
 
45
    initpos = Pos 1 1 filename
 
46
    
 
47
    go inp@(pos, cs)
 
48
      = case alexScan inp 0 of
 
49
          AlexEOF         -> []
 
50
          AlexError inp'  -> valueToken TkError [head cs] pos : go inp'
 
51
          AlexSkip inp' _ -> go inp'
 
52
          AlexToken inp' len act -> act (take len cs) pos : go inp'
 
53
}
 
54