From 5729bc0ea96fc0b944c243230910c537d692bcad Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Sat, 17 Nov 2018 19:11:03 +0200 Subject: implement the second homework assignment (#39) * Rust syntax highlighter: fix some issues with the implementation of the first homework assignment * Rust syntax highlighter: pretty-printer: implement the second homework assignment --- .../81030/1,2-rust2html/rust2html-prettyprint.l | 344 +++++++++++++++++++++ 1 file changed, 344 insertions(+) create mode 100644 homework/81030/1,2-rust2html/rust2html-prettyprint.l (limited to 'homework/81030/1,2-rust2html/rust2html-prettyprint.l') diff --git a/homework/81030/1,2-rust2html/rust2html-prettyprint.l b/homework/81030/1,2-rust2html/rust2html-prettyprint.l new file mode 100644 index 0000000..2d90931 --- /dev/null +++ b/homework/81030/1,2-rust2html/rust2html-prettyprint.l @@ -0,0 +1,344 @@ +%option nounistd +%option noyywrap +%option stack + +%{ +#include + +#define INDENTATION " " +#define SPACE " " +#define HIGHLIGHT_TERMINATOR "" + +#define PRINT_HIGHLIGHTED_TOKEN_INITIATOR(type) \ + printf("%s", yytext) + +#define PRINT_HIGHLIGHTED_TOKEN_TERMINATOR \ + printf("%s" HIGHLIGHT_TERMINATOR, yytext) + +#define PRINT_HIGHLIGHTED_TOKEN(type) \ + printf("%s", yytext) + +#define PRINT_TOKEN \ + printf("%s", yytext) + +#define PRINT_INDENTED_NEWLINE \ + printf("\n"); \ + for (int i = 0; i < indentation_level; i++) \ + printf(INDENTATION) + +#define CLEAR_MODES \ + require_space_mode = false; \ + adjacent_parenthesis_mode = false + +#define HANDLE_COMMENT_INITIATOR(type) \ + PRINT_HIGHLIGHTED_TOKEN_INITIATOR(comment); \ + yy_push_state(type); \ + CLEAR_MODES + +#define HANDLE_LITERAL(type) \ + if (require_space_mode) \ + printf(SPACE); \ + PRINT_HIGHLIGHTED_TOKEN(type); \ + require_space_mode = true; \ + adjacent_parenthesis_mode = false + +#define HANDLE_IDENTIFIER \ + if (require_space_mode) \ + printf(SPACE); \ + PRINT_HIGHLIGHTED_TOKEN(identifier); \ + require_space_mode = true; \ + adjacent_parenthesis_mode = true + +#define HANDLE_EOL \ + yy_pop_state(); \ + adjacent_parenthesis_mode = true; \ + yyless(0) + +static int indentation_level = 0; +static bool require_space_mode = false; +static bool adjacent_parenthesis_mode = false; +%} + +%s multiline_list +%s path +%s directive + +%x line_comment +%x block_comment +%x eol + +NONEOL_CHAR . +DEC_DIGIT 0|{NONZERO_DEC_DIGIT} +NONZERO_DEC_DIGIT [1-9] +OCT_DIGIT [0-7] +HEX_DIGIT {DEC_DIGIT}|[a-fA-F] + +IDENTIFIER {IDENTIFIER_FIRST_CHAR}({DEC_DIGIT}|{IDENTIFIER_FIRST_CHAR})* +IDENTIFIER_FIRST_CHAR [A-Za-z_] + +LINE_COMMENT_BEGIN "//" +BLOCK_COMMENT_INITIATOR "/*" +BLOCK_COMMENT_TERMINATOR "*/" + +KEYWORD _|abstract|alignof|as|become|box|break|const|continue|crate|do|else|enum|extern|false|final|fn|for|if|impl|in|let|loop|macro|match|mod|move|mut|offsetof|override|priv|proc|pub|pure|ref|return|Self|self|sizeof|static|struct|super|trait|true|type|typeof|unsafe|unsized|use|virtual|where|while|yield + +ESCAPE {COMMON_ESCAPE}|{UNICODE_ESCAPE} +COMMON_ESCAPE \\|n|r|t|0|x{HEX_DIGIT}{2} +UNICODE_ESCAPE "u{"{HEX_DIGIT}{6}"}" + +CHAR_LITERAL '{CHAR_BODY}' +CHAR_BODY [^']|(\\('|{ESCAPE})) +STRING_LITERAL \"{STRING_BODY}*\" +STRING_BODY [^\"]|(\\(\"|{ESCAPE})) +BYTE_CHAR_LITERAL b'{BYTE_CHAR_BODY}' +BYTE_CHAR_BODY [^']|(\\('|{COMMON_ESCAPE})) +BYTE_STRING_LITERAL b\"{BYTE_STRING_BODY}*\" +BYTE_STRING_BODY [^\"]|(\\(\"|{ESCAPE})) + +NUMBER_LITERAL {NONZERO_DEC_DIGIT}{DEC_DIGIT}*{FLOAT_SUFFIX}?|(0{DEC_DIGIT_LITERAL}*{FLOAT_SUFFIX}?|b[10_]+|o{OCT_DIGIT_LITERAL}+|x{HEX_DIGIT_LITERAL}+) +FLOAT_SUFFIX {EXPONENT}|"."{DEC_DIGIT_LITERAL}+{EXPONENT}? +EXPONENT [Ee][-+]?{DEC_DIGIT_LITERAL}+ +DEC_DIGIT_LITERAL {DEC_DIGIT}|_ +HEX_DIGIT_LITERAL {HEX_DIGIT}|_ +OCT_DIGIT_LITERAL {OCT_DIGIT}|_ + +%% + +[[:space:]]+ + +";"|"}" { + HANDLE_EOL; +} + +"else" { + HANDLE_EOL; + printf(SPACE); +} + +. { + HANDLE_EOL; + PRINT_INDENTED_NEWLINE; +} + +[[:blank:]]*\n { + yy_pop_state(); + printf(HIGHLIGHT_TERMINATOR); + yy_push_state(eol); +} + +{BLOCK_COMMENT_TERMINATOR} { + yy_pop_state(); + PRINT_HIGHLIGHTED_TOKEN_TERMINATOR; + yy_push_state(eol); +} + +. { + PRINT_TOKEN; +} + +(.|\n) { + PRINT_TOKEN; +} + +[[:space:]]+ + +"use" { + HANDLE_LITERAL(keyword); + yy_push_state(path); +} + +{KEYWORD} { + HANDLE_LITERAL(keyword); +} + +{IDENTIFIER} { + HANDLE_IDENTIFIER; +} + +{NUMBER_LITERAL} { + HANDLE_LITERAL(number); +} + +{CHAR_LITERAL}|{STRING_LITERAL}|{BYTE_CHAR_LITERAL}|{BYTE_STRING_LITERAL} { + HANDLE_LITERAL(string); +} + +{LINE_COMMENT_BEGIN} { + HANDLE_COMMENT_INITIATOR(line_comment); +} + +{BLOCK_COMMENT_INITIATOR} { + HANDLE_COMMENT_INITIATOR(block_comment); +} + +"," { + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; + yy_push_state(eol); +} + +","|":" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + require_space_mode = true; + adjacent_parenthesis_mode = false; +} + +";" { + yy_pop_state(); + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; + yy_push_state(eol); +} + +";" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; + yy_push_state(eol); +} + +"#" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; + yy_push_state(directive); +} + +"?" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + require_space_mode = true; + adjacent_parenthesis_mode = false; +} + +"!" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + require_space_mode = false; + adjacent_parenthesis_mode = true; +} + +"."|"::"|"'"|"$"|"@"|"~" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; +} + +"=="|"=>"|"="|"!="|"<="|"<<"|"<<="|"<"|">="|">>"|">>="|">"|"<-"|"->"|"-"|"-="|"&&"|"&"|"&="|"||"|"|"|"|="|"+"|"+="|"*"|"*="|"/"|"/="|"^"|"^="|"%"|"%="|".."|"..." { + printf(SPACE); + PRINT_HIGHLIGHTED_TOKEN(symbol); + require_space_mode = true; + adjacent_parenthesis_mode = false; +} + +"(" { + if (!adjacent_parenthesis_mode) + printf(SPACE); + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; + yy_push_state(INITIAL); +} + +"[" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; +} + +"[" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; + yy_push_state(INITIAL); +} + +"]" { + yy_pop_state(); + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; + yy_push_state(eol); +} + +")"|"]" { + yy_pop_state(); + PRINT_HIGHLIGHTED_TOKEN(symbol); + require_space_mode = true; + adjacent_parenthesis_mode = false; +} + +"{" { + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; +} + +"{" { + if (require_space_mode) + printf(SPACE); + indentation_level++; + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; + yy_push_state(multiline_list); + yy_push_state(eol); +} + +"}" { + yy_pop_state(); + PRINT_HIGHLIGHTED_TOKEN(symbol); + CLEAR_MODES; +} + +"}" { + yy_pop_state(); + indentation_level--; + PRINT_INDENTED_NEWLINE; + PRINT_HIGHLIGHTED_TOKEN(symbol); + if (0 == indentation_level) + printf("\n"); + CLEAR_MODES; + yy_push_state(eol); +} + +. { + PRINT_TOKEN; /* echo the rest */ +} + +%% + +int main(int argc, const char* const argv[]) { + yyin = argc > 1 ? fopen(argv[1], "r") : stdin; + + printf( + "\n" + "\n" + "\n" + "\n" + " \n" + "\n" + "\n" + "\n" + "
\n"
+    );
+    yylex();
+    printf(
+        "\n"
+        "    
\n" + "\n" + "\n" + "" + ); + + return 0; +} -- cgit v1.3.1-26-gb76a