Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81030 / rust2html.flex
diff options
context:
space:
mode:
Commit3075429adc93c18c72a77a311056debe89342aca (patch)
Author
 Radoslav Georgiev <rgeorgiev583@gmail.com>
2018-10-21 13:37:26 +0300
Committer
 Martin Dobrev <martindobrev0@gmail.com>
2018-10-21 13:37:26 +0300
Files1c4bd705209218f11f785bdb33c74b06bb449ef5 /homework/81030/rust2html.flex
IPL-3075429adc93c18c72a77a311056debe89342aca.tar
IPL-3075429adc93c18c72a77a311056debe89342aca.tar.gz
IPL-3075429adc93c18c72a77a311056debe89342aca.zip
Parent4366cb1f8e2c8c57b108e5b3f07ab451c1a7112d (diff)
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
1 files changed, 115 insertions, 0 deletions
-rw-r--r--homework/81030/rust2html.flex115
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("<span class=\"symbol\">%s</span>", yytext);
+}
+
+{KEYWORD} {
+ printf("<span class=\"keyword\">%s</span>", yytext);
+}
+
+{NUMBER_LITERAL} {
+ printf("<span class=\"number\">%s</span>", yytext);
+}
+
+{IDENTIFIER} {
+ printf("<span class=\"identifier\">%s</span>", yytext);
+}
+
+{CHAR_LITERAL}|{STRING_LITERAL}|{BYTE_CHAR_LITERAL}|{BYTE_STRING_LITERAL} {
+ printf("<span class=\"string\">%s</span>", yytext);
+}
+
+{COMMENT} {
+ printf("<span class=\"comment\">%s</span>", 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(
+ "<!DOCTYPE html>"
+ "<html>"
+ "<head>"
+ " <style>"
+ " .comment {"
+ " color: gray;"
+ " }"
+ " .keyword {"
+ " color: red;"
+ " }"
+ " .string {"
+ " color: green;"
+ " }"
+ " .number {"
+ " color: blue;"
+ " }"
+ " .symbol {"
+ " font-weight: bold;"
+ " }"
+ " </style>"
+ "</head>"
+ "<body>"
+ " <pre class=\"code\">"
+ );
+ yylex();
+ puts(
+ " </pre>"
+ "</body>"
+ "</html>");
+
+ return 0;
+}