From 3075429adc93c18c72a77a311056debe89342aca Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Sun, 21 Oct 2018 13:37:26 +0300 Subject: implement the first homework assignment (#8) * Rust syntax highlighter: add incomplete implementation of a scanner for the Rust programming language * Rust syntax highlighter: fix some issues with unrecognized rules caused by unescaped literals * Rust syntax highlighter: refactor the definitions section, fixing some escaping-related style issues * Rust syntax highlighter: refactor the definitions and rules sections, abstracting out some hardcoded patterns * Rust syntax highlighter: refactor, reordering the definitions, rules and CSS class definitions * Rust syntax highlighter: refactor, fixing some style issues with the C code * Rust syntax highlighter: refactor the code section, fixing some HTML- and CSS-related issues * Rust syntax highlighter: drop the document title from the output as it is wrongly hardcoded * Rust syntax highlighter: add gitignore * Rust syntax highlighter: add C source of generated scanner * Rust syntax highlighter: add example Rust program --- homework/81030/rust2html.flex | 115 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 homework/81030/rust2html.flex (limited to 'homework/81030/rust2html.flex') diff --git a/homework/81030/rust2html.flex b/homework/81030/rust2html.flex new file mode 100644 index 0000000..6684d82 --- /dev/null +++ b/homework/81030/rust2html.flex @@ -0,0 +1,115 @@ +/* scanner for the Rust programming language */ + +%{ +#define YY_NO_UNISTD_H +%} + +NONEOL_CHAR [^\n] +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_] + +COMMENT {BLOCK_COMMENT}|{LINE_COMMENT} +BLOCK_COMMENT "/*".*"*/" +LINE_COMMENT "//"{NONEOL_CHAR}* + +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}|_ + +SYMBOL "::"|"->"|"#"|"["|"]"|"("|")"|"{"|"}"|","|";" + +%% + +{SYMBOL} { + printf("%s", yytext); +} + +{KEYWORD} { + printf("%s", yytext); +} + +{NUMBER_LITERAL} { + printf("%s", yytext); +} + +{IDENTIFIER} { + printf("%s", yytext); +} + +{CHAR_LITERAL}|{STRING_LITERAL}|{BYTE_CHAR_LITERAL}|{BYTE_STRING_LITERAL} { + printf("%s", yytext); +} + +{COMMENT} { + printf("%s", yytext); +} + +. { + printf("%s", yytext); /* echo the rest */ +} + +%% + +int yywrap() { + return 1; +} + +int main(int argc, const char* argv[]) { + yyin = argc > 1 ? fopen(argv[1], "r") : stdin; + + puts( + "" + "" + "" + " " + "" + "" + "
"
+    );
+    yylex();
+    puts(
+        "    
" + "" + ""); + + return 0; +} -- cgit v1.3.1-26-gb76a