Commit 613c96d2bcbedc049b46fb70411571f5704d4dc3

Authored by Alain Prouté
1 parent eea321c1

add fast lexer 3 example: searching href into given file in argument

anubis_dev/library/examples/lexical_analysis/href_extractor.anubis 0 → 100644
  1 +read tools/basis.anubis
  2 +
  3 +read lexical_analysis/fast_lexer_3.anubis
  4 +
  5 +define List(LexerItem(One, One))
  6 + href_lexer_description =
  7 + [
  8 + lexer_item("href[# #n#r]*=[# #n#r]*#\"", return((ByteArray b, LexingTools t, One u) |-> token(unique))),
  9 + lexer_item(".", ignore),
  10 + lexer_item("#n", ignore)
  11 + ].
  12 +
  13 +define List(LexerItem(String, One))
  14 + href_content_lexer_description =
  15 + [
  16 + lexer_item("[^\"]*", return((ByteArray b, LexingTools t, One u) |-> token(to_string(b))))
  17 + ].
  18 +
  19 +global define One
  20 + href_extractor
  21 + (
  22 + List(String) args
  23 + ) =
  24 + if args is
  25 + {
  26 + [ ] then unique,
  27 + [filename . _] then
  28 + if file(filename, read) is
  29 + {
  30 + failure then unique,
  31 + success(fd) then
  32 + if make_lexer(href_lexer_description, '#') is
  33 + {
  34 + error(e) then print(to_English(e) + "\n"),
  35 + ok(href_lexer) then
  36 + if make_lexer(href_content_lexer_description, '#') is
  37 + {
  38 + error(e) then print(to_English(e) + "\n"),
  39 + ok(href_content_lexer) then
  40 + with mb_ls = make_lexing_stream("", fd, 16384, 10, unique),
  41 +
  42 + if mb_ls is
  43 + {
  44 + failure then println("make_lexing_stream failed"),
  45 + success(ls) then
  46 + with cb1 = href_lexer(ls),
  47 + cb2 = href_content_lexer(ls),
  48 + ((One u) |-cb3->
  49 + if cb1(u) is
  50 + {
  51 + end_of_input then println("end of input"),
  52 + error(_,l,c) then println("error at line " + l + " col " + c),
  53 + token(t) then
  54 + if cb2(unique) is
  55 + {
  56 + end_of_input then println("end of input"),
  57 + error(_,l1,c1) then println("error at line " + l1 + " col " + c1),
  58 + token(t1) then
  59 + println("href found " + t1);
  60 +
  61 + cb3(unique)
  62 + }
  63 + })(unique)
  64 + }
  65 + }
  66 + }
  67 + }
  68 + }.
... ...