diff --git a/anubis_dev/library/syntactic_analysis/common.anubis b/anubis_dev/library/syntactic_analysis/common.anubis index 5efb4a2..3548f68 100644 --- a/anubis_dev/library/syntactic_analysis/common.anubis +++ b/anubis_dev/library/syntactic_analysis/common.anubis @@ -43,6 +43,23 @@ public type APG_Error: bad_end_of_precedence_declaration (Int32 line), bad_end_of_type_declaration (Int32 line). + + + + + *Name* en_print + + *Description* The function `en_print` prints in English a message of type `APG_Error` + on the standard output. + +public define One + en_print + ( + APG_Error e + ). + + + @@ -51,25 +68,47 @@ public type APG_Error: *Description* This is the type of (abstract) grammars. An object of this type is produced by the - function `read_APG_grammar`, and given as an argument to the function - `make_APG_automaton` (see `make_automaton.anubis`). This type is opaque. + function `read_APG_grammar` (see `read_grammar.anubis`), and given as an argument to + the function `make_APG_automaton` (see `make_automaton.anubis`). This type is opaque. public type APG_Grammar:... + + *Name* print_grammar + + *Description* + + This function prints a grammar (of type `APG_Grammar`) on the standard output. The + preambule and postambule are not printed. + +public define One + print_grammar + ( + APG_Grammar g + ). + + + + + *Name* APG_Automaton: This type represents abstract automatons as they are constructed by the function - `make_APG_automaton`. This is an opaque type. + `make_APG_automaton` (see `make_automaton.anubis`). This is an opaque type. public type APG_Automaton:... + + *Private* + +read tools/basis.anubis public type APG_Precedence_Dec: left (List(String) symbol_names), @@ -103,4 +142,116 @@ public type APG_Automaton: . - \ No newline at end of file +public define One + en_print + ( + APG_Error e + ) = + if e is + { + unexpected_end_of_input(Int32 line) then + print(integer_to_string(line)+": Unexpected end of input.\n"), + + no_parser_name(Int32 line) then + print(integer_to_string(line)+": No parser name.\n"), + + keyword_was_expected(Int32 line) then + print(integer_to_string(line)+": Keyword was expected.\n"), + + unknown_declaration_keyword(Int32 line,String keyword) then + print(integer_to_string(line)+": Unknown declaration keyword '"+keyword+"'.\n"), + + type_description_expected(Int32 line) then + print(integer_to_string(line)+": Type description expected.\n"), + + incorrect_symbol_value(Int32 line) then + print(integer_to_string(line)+": Incorrect symbol value.\n"), + + symbol_expected(Int32 line) then + print(integer_to_string(line)+": Symbol expected.\n"), + + colon_expected(Int32 line) then + print(integer_to_string(line)+": Colon expected.\n"), + + incorrect_rule_precedence(Int32 line) then + print(integer_to_string(line)+": Incorrect rule precedence.\n"), + + misclosed_rule_precedence(Int32 line) then + print(integer_to_string(line)+": Misclosed rule precedence.\n"), + + incorrect_end_of_rule(Int32 line) then + print(integer_to_string(line)+": Incorrect end of rule.\n"), + + bad_end_of_precedence_declaration(line) then + print(integer_to_string(line)+": Incorrect end of precedence declaration.\n"), + + bad_end_of_type_declaration(line) then + print(integer_to_string(line)+": Incorrect end of type declaration.\n"), + }. + + + + +define One + print + ( + APG_Precedence_Dec d + ) = + if d is + { + left(names) then map_forget((String s) |-> print("left "+s+"\n"),names), + right(names) then map_forget((String s) |-> print("right "+s+"\n"),names), + non_assoc(names) then map_forget((String s) |-> print("non_assoc "+s+"\n"),names) + }. + + +define One + print + ( + APG_Type_Dec d + ) = + if d is type_dec(type,names) then + map_forget((String s) |-> print(type+" "+s+"\n"),names). + + + +define One + print + ( + APG_Symbol_Value sv + ) = + if sv is symbol_value(name,mb_value) + then print(name+" "+ if mb_value is + { + failure then "", + success(value) then "{"+value+"} " + }). + + +define One + print + ( + APG_Grammar_Rule r + ) = + if r is grammar_rule(id,head,body,prec) then + print("["+id+"] "); + print(head); + print(": "); + map_forget(print,body); + print("\n"). + +public define One + print_grammar + ( + APG_Grammar g + ) = + if g is grammar(preambule, + parser_name, + precedence_declarations, + type_declarations, + grammar_rules, + postambule) then + print("Parser name: "+parser_name+"\n"); + map_forget(print,precedence_declarations); + map_forget(print,type_declarations); + map_forget(print,grammar_rules). \ No newline at end of file diff --git a/anubis_dev/library/syntactic_analysis/read_grammar.anubis b/anubis_dev/library/syntactic_analysis/read_grammar.anubis index 353e6d5..31faed2 100644 --- a/anubis_dev/library/syntactic_analysis/read_grammar.anubis +++ b/anubis_dev/library/syntactic_analysis/read_grammar.anubis @@ -76,11 +76,10 @@ define $U -> Result($E,$W) - This one (the 'Kleisli applicator') does'nt work because in Anubis 1 we cannot overload - the applicator. This will work only in Anubis 2. + A variant: the 'Kleisli applicator': - define Result($E,$W) - ($U -> Result($E,$W) f)(Result($E,$U) x) // this syntax is not accepted by Anubis 1 +define Result($E,$W) + $U -> Result($E,$W) f * Result($E,$U) x = if x is { @@ -210,8 +209,7 @@ define Result(APG_Error,String) if read_symbol(s) is { failure then error(no_parser_name(current_line(s))), - success(parser_name) then print("parser name: <"+parser_name+">\n"); - ok(parser_name) + success(parser_name) then ok(parser_name) }. @@ -271,7 +269,7 @@ define Maybe(String) define Maybe(String) read_type_of_dec ( - Stream s + Stream s ) = skip_blanks_and_comments(s); if read_byte(s) is @@ -316,18 +314,13 @@ define Result(APG_Error,Dec_Item) { failure then error(unexpected_end_of_input(current_line(s))), success(byte) then - print("col1: "+integer_to_string(current_column(s))+"\n"); - print("lin1: "+integer_to_string(current_line(s))+"\n"); if byte = '#' then ok(separator) else unput_byte(byte,s); - print("col2: "+integer_to_string(current_column(s))+"\n"); - print("lin2: "+integer_to_string(current_line(s))+"\n"); if read_symbol(s) is { failure then error(keyword_was_expected(current_line(s))), success(keyword) then - print("Keyword: "+keyword+"\n"); if keyword = "type" then (((APG_Type_Dec d) |-> ok(type_dec(d))) * read_type_dec)(s) else with g = (APG_Precedence_Dec d) |-> (Result(APG_Error,Dec_Item))ok(prec_dec(d)), @@ -472,11 +465,12 @@ define Result(APG_Error,APG_Grammar_Rule) then ok(grammar_rule(rule_id,_A,reverse(so_far),failure)) else if byte = '[' // precedence then read_rule_precedence(s,_A,rule_id,reverse(so_far)) - else if read_symbol_and_value(s) is - { - error(msg) then error(msg), - ok(sv) then read_grammar_rule_tail(s,_A,rule_id,[sv . so_far]) - } + else unput_byte(byte,s); + if read_symbol_and_value(s) is + { + error(msg) then error(msg), + ok(sv) then read_grammar_rule_tail(s,_A,rule_id,[sv . so_far]) + } }. @@ -619,54 +613,6 @@ public define Result(APG_Error,APG_Grammar) *** [8] Testing the grammar reader. -define One - print - ( - APG_Error e - ) = - if e is - { - unexpected_end_of_input(Int32 line) then - print(integer_to_string(line)+": Unexpected end of input.\n"), - - no_parser_name(Int32 line) then - print(integer_to_string(line)+": No parser name.\n"), - - keyword_was_expected(Int32 line) then - print(integer_to_string(line)+": Keyword was expected.\n"), - - unknown_declaration_keyword(Int32 line,String keyword) then - print(integer_to_string(line)+": Unknown declaration keyword '"+keyword+"'.\n"), - - type_description_expected(Int32 line) then - print(integer_to_string(line)+": Type description expected.\n"), - - incorrect_symbol_value(Int32 line) then - print(integer_to_string(line)+": Incorrect symbol value.\n"), - - symbol_expected(Int32 line) then - print(integer_to_string(line)+": Symbol expected.\n"), - - colon_expected(Int32 line) then - print(integer_to_string(line)+": Colon expected.\n"), - - incorrect_rule_precedence(Int32 line) then - print(integer_to_string(line)+": Incorrect rule precedence.\n"), - - misclosed_rule_precedence(Int32 line) then - print(integer_to_string(line)+": Misclosed rule precedence.\n"), - - incorrect_end_of_rule(Int32 line) then - print(integer_to_string(line)+": Incorrect end of rule.\n"), - - bad_end_of_precedence_declaration(line) then - print(integer_to_string(line)+": Incorrect end of precedence declaration.\n"), - - bad_end_of_type_declaration(line) then - print(integer_to_string(line)+": Incorrect end of type declaration.\n"), - }. - - global define One grammar_read_test ( @@ -682,8 +628,9 @@ global define One success(f) then if read_APG_grammar(make_stream(f)) is { - error(msg) then print(msg), - ok(g) then print("Got a grammar !\n") + error(msg) then en_print(msg), + ok(g) then print("Got a grammar !\n"); + print_grammar(g) } } }. -- libgit2 0.21.4