/* * 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 = 43 then email_tester_state_4(_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 = 43 then email_tester_state_4(_ls) else 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 = 43 then email_tester_state_6(_ls) else 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 = 43 then email_tester_state_6(_ls) else 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 }.