fqa.anubis 13.4 KB
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 12/07/2017
 * Time: 22:28
 * © David RENÉ
 */



   
   This is an example of use of 'lexer_maker'. 
   
read tools/basis.anubis   

   
   We  want   to  test  email   addresses.  Below  is   a  regular  expression   for  that
   purpose. Actually, this expression is too naïve. A real one would be more complicated. 
   

read tools/streams.anubis   

type LM_TokenOrError_FQA:
  end_of_file, 
  token(List(Word8)),
  error. 

type LM_LexerState_FQA: ...

type LM_Match_FQA:
  match(List(Word8) characters, 
        (LM_LexerState_FQA,List(Word8)) -> (LM_LexerState_FQA,LM_TokenOrError_FQA) action,
        Bool aeol,
        Bool abol). 

type LM_LexerState_FQA:
  lexer_state(Stream                 input,
              List(Word8)             unput,         // in natural order
              List(Word8)             more,          // in reverse order
              LM_LexerState_FQA -> (LM_LexerState_FQA,LM_TokenOrError_FQA) lexer, 
              Bool                   at_end_of_line,
              Bool                   at_beginning_of_line,
              Maybe(LM_Match_FQA)        match). 

define LM_LexerState_FQA
  lm_initial_state
    (
      Stream input,
      LM_LexerState_FQA -> (LM_LexerState_FQA,LM_TokenOrError_FQA) lexer
    ) =
  lexer_state(input,[],[],lexer,false,true,failure). 

define (LM_LexerState_FQA,Word8)
  lm_next_char
    (
      LM_LexerState_FQA ls
    ) =
  if ls is lexer_state(input,unput,more,lex,aeol,abol,match) then 
  if unput is
    {
      [ ] then
        if read_byte(input) is 
          {
            failure then
              (lexer_state(input,
                           [],
                           more,
                           lex,
                           true,
                           aeol,
                           match),
               -1),

            success(c) then 
              (lexer_state(input,
                           [],
                           [c . more],
                           lex,
                           c = '\n',
                           aeol,
                           match),
               c),
          },
               
      [h . t] then 
        (lexer_state(input,
                     t,
                     [h . more],
                     lex,
                     h = '\n',
                     aeol,
                     match),
         h)
    }. 

define LM_LexerState_FQA
  lm_remember_match
    (
      (LM_LexerState_FQA,List(Word8)) -> (LM_LexerState_FQA,LM_TokenOrError_FQA) action,
      LM_LexerState_FQA ls
    ) =
  if ls is lexer_state(input,unput,more,lex,aeol,abol,m) then 
  with chars = if m is 
    {
      failure then [], 
      success(match) then 
        if match is match(chars,_,_,_) then chars
    },
  lexer_state(input,
              unput,
              [],
              lex,
              aeol,
              abol,
              success(match(append(more,chars),action,aeol,abol))).

define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  lm_find_match
    (
      LM_LexerState_FQA ls
    ) =
  if ls is lexer_state(input,unput,more,lex,_,_,mb_m) then 
  if mb_m is 
    {
      failure then (ls,error), 
      success(m) then
        if m is match(chars,action,aeol,abol) then 
           action(lexer_state(input,
                              append(more,unput),
                              [],
                              lex,
                              aeol,
                              abol,
                              failure),
                  reverse(chars))
    }.

define LM_LexerState_FQA
  change_lexer
    (
      LM_LexerState_FQA ls, 
      LM_LexerState_FQA -> (LM_LexerState_FQA,LM_TokenOrError_FQA) lex
    ) =
  if ls is lexer_state(input,unput,more,_,aeol,abol,mb_m) then 
  lexer_state(input,unput,more,lex,aeol,abol,mb_m). 

define LM_LexerState_FQA
  clear_abol
    (
      LM_LexerState_FQA ls
    ) =
  if ls is lexer_state(input,unput,more,lex,aeol,_,mb_m) then 
  lexer_state(input,unput,more,lex,aeol,false,mb_m). 

   Declarations of all lexer states.


public define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_1(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_2(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_3(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_4(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_5(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_6(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_7(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_8(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_9(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_10(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_11(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_12(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_state_13(LM_LexerState_FQA ls).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_action_0
   (LM_LexerState_FQA ls, List(Word8) text).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_action_1
   (LM_LexerState_FQA ls, List(Word8) text).
define (LM_LexerState_FQA,LM_TokenOrError_FQA) email_tester_action_2
   (LM_LexerState_FQA ls, List(Word8) text).


   Lexer states.


public define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester
    (
      LM_LexerState_FQA ls,
    ) =
  if at_beginning_of_line(ls)
  then email_tester_state_3(clear_abol(ls)) else
  if lm_next_char(ls) is (_ls,c) then
  if c = -1 then email_tester_state_1(_ls) else
  if c = 45 then email_tester_state_4(_ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_4(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_4(_ls) else
  if c = 95 then email_tester_state_4(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_4(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_1
    (
      LM_LexerState_FQA ls,
    ) =
  with _ls = lm_remember_match(email_tester_action_2,ls),
  if lm_next_char(_ls) is (__ls,c) then
  lm_find_match(__ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_2
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_3
    (
      LM_LexerState_FQA ls,
    ) =
  with _ls = lm_remember_match(email_tester_action_1,ls),
  if lm_next_char(_ls) is (__ls,c) then
  lm_find_match(__ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_4
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  if c = 45 then email_tester_state_4(_ls) else
  if c = 46 then email_tester_state_5(_ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_4(_ls) else
  if c = 64 then email_tester_state_7(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_4(_ls) else
  if c = 95 then email_tester_state_4(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_4(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_5
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  if c = 45 then email_tester_state_6(_ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_6(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_6(_ls) else
  if c = 95 then email_tester_state_6(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_6(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_6
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  if c = 45 then email_tester_state_6(_ls) else
  if c = 46 then email_tester_state_5(_ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_6(_ls) else
  if c = 64 then email_tester_state_7(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_6(_ls) else
  if c = 95 then email_tester_state_6(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_6(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_7
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  if c = 45 then email_tester_state_8(_ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_8(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_8(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_8(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_8
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  if c = 45 then email_tester_state_8(_ls) else
  if c = 46 then email_tester_state_9(_ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_8(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_8(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_8(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_9
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  if c = 45 then email_tester_state_10(_ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_11(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_11(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_11(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_10
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  if c = 45 then email_tester_state_10(_ls) else
  if c = 46 then email_tester_state_9(_ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_10(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_10(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_10(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_11
    (
      LM_LexerState_FQA ls,
    ) =
  with _ls = lm_remember_match(email_tester_action_0,ls),
  if lm_next_char(_ls) is (__ls,c) then
  if c = 45 then email_tester_state_10(__ls) else
  if c = 46 then email_tester_state_12(__ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_11(__ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_11(__ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_11(__ls) else
  lm_find_match(__ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_12
    (
      LM_LexerState_FQA ls,
    ) =
  if lm_next_char(ls) is (_ls,c) then
  if c = 45 then email_tester_state_10(ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_13(_ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_13(_ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_13(_ls) else
  lm_find_match(_ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_state_13
    (
      LM_LexerState_FQA ls,
    ) =
  with _ls = lm_remember_match(email_tester_action_0,ls),
  if lm_next_char(_ls) is (__ls,c) then
  if c = 45 then email_tester_state_10(__ls) else
  if c = 46 then email_tester_state_12(__ls) else
  if (48 +=< c & c +=< 57) then email_tester_state_13(__ls) else
  if (65 +=< c & c +=< 90) then email_tester_state_13(__ls) else
  if (97 +=< c & c +=< 122) then email_tester_state_13(__ls) else
  lm_find_match(__ls).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_action_0
    (
      LM_LexerState_FQA ls,
      List(Word8) text
    ) =
  (ls,token(text)).


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_action_1
    (
      LM_LexerState_FQA ls,
      List(Word8) text
    ) =
   /* default action */ email_tester(ls) .


define (LM_LexerState_FQA,LM_TokenOrError_FQA)
  email_tester_action_2
    (
      LM_LexerState_FQA ls,
      List(Word8) text
    ) =
   /* default action */ (ls,end_of_file) .


   Since '@' is  a normal character, a string  needs to contain exactly one  '@' for being
   accepted. What is accepted before and after this '@' is described by:
   
          [a-zA-Z]+(\.[a-zA-Z]+)*
   
   The first part: [a-zA-Z]+ means ``at  least one letter''. The last part: (\.[a-zA-Z]+)*
   means: ``a dot followed by at least one  letter, and this may be repeated any number of
   times (including zero)''.
   
   
   This part of the source file is the 'postambule' (just Anubis text, which is copied 'as
   is' to the lexer_maker output file).
   
   The above  stuff produces a function  named 'email_tester' into  the lexer_maker output
   file. This function is used below:
   
public define Bool
  test_fqa
  (
    String account
  ) =
  with ls = lexer_state(make_stream(account),[],[],email_tester,true,false,failure), 
    if email_tester(ls) is (_,result) then if result is
    {
      end_of_file      then false,
      token(t)         then
      (
        with _result = implode(t),
          if length(_result) = length(account) then 
            //logDebug(debug_log,"["+account +"] is FQA valid");
            true
          else 
            println("["+account+"] is valid FQA but different than ["+_result+"]"); 
            false
      ),
      error            then 
        //logWarning(debug_log,"["+account+"] isn't valid FQA "); 
        false
    }.