Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commita4c91889a67ec12057d08d21a81a1f4ed677fc67 (patch)
Author
 Georgi Stefanov <geosteffanov@gmail.com>
2018-10-25 12:54:32 +0300
Committer
 dtrendafilov <trendafilov.dn@gmail.com>
2018-10-25 12:54:32 +0300
Files1d2e78b4bf2fc38a033b5f22359f123dbc152108
IPL-a4c91889a67ec12057d08d21a81a1f4ed677fc67.tar
IPL-a4c91889a67ec12057d08d21a81a1f4ed677fc67.tar.gz
IPL-a4c91889a67ec12057d08d21a81a1f4ed677fc67.zip
Parent68964c78790c0eaf847fa0491f85d73566e4985d (diff)
81319 golang2html lexer (#20)
* Add highlighter for golang * Add readme with instructions * Fix readme
3 files changed, 172 insertions, 0 deletions
-rw-r--r--homework/81319/golang2html/README.md30
-rw-r--r--homework/81319/golang2html/golang2html.flex121
-rw-r--r--homework/81319/golang2html/hello_world.go21
diff --git a/homework/81319/golang2html/README.md b/homework/81319/golang2html/README.md
new file mode 100644
index 0000000..63c5f53
--- /dev/null
+++ b/homework/81319/golang2html/README.md
@@ -0,0 +1,30 @@
+# How-to guide
+
+## Compiling the highlighter
+
+1. Transpile flex file to c file
+```
+flex golang2html.flex
+```
+
+2. Compile the produced output file
+
+```
+gcc lex.yy.c -o lexer
+```
+
+The produced output is the 'lexer' file. You use it to highlight
+your own golang source files.
+
+## Highlighting Golang code
+
+1. Create your own golang source file with whatever name you wish. For
+this example, let your golang source be 'hello_world.go'.
+
+2. Produce the highlighted html output
+
+```
+./lexer hello_world.go > hello_world.html
+```
+
+## Enjoy
diff --git a/homework/81319/golang2html/golang2html.flex b/homework/81319/golang2html/golang2html.flex
new file mode 100644
index 0000000..9501645
--- /dev/null
+++ b/homework/81319/golang2html/golang2html.flex
@@ -0,0 +1,121 @@
+/* golang scanner */
+
+%{
+#define YY_NO_UNISTD_H
+%}
+
+DIGIT [0-9]
+ID [a-zA-Z_][a-zA-Z0-9_]*
+STRQ '([^\']|(\\\'))*'
+STRDQ \"([^\"]|(\\\"))*\"
+
+%%
+
+{DIGIT}+ {
+ printf("<span class=\"number\">%s</span>", yytext);
+ }
+
+{DIGIT}+"."{DIGIT}* {
+ printf("<span class=\"number\">%s</span>", yytext);
+ }
+
+break|default|func|interface|select|case|defer|go|map|struct {
+ printf("<span class=\"keyword\">%s</span>", yytext);
+}
+
+chan|else|goto|package|switch|const|fallthrough|if|range|type|continue|for|import|return|var {
+ printf("<span class=\"keyword\">%s</span>", yytext);
+}
+
+{ID} printf("<span class=\"identifier\">%s</span>", yytext);
+{STRQ}|{STRDQ} printf("<span class=\"string\">%s</span>", yytext);
+
+"+"|"&"|"+="|"&="|"&&"|"=="|"!="|"-"|"|"|"-="|"|="|"||"|"<" {
+ printf("<span class=\"operator\">%s</span>", yytext);
+}
+
+"<="|"["|"]"|"*"|"^"|"*="|"^="|"<-"|">"|">="|"{"|"}"|"/"|"<<"|"/=" {
+ printf("<span class=\"operator\">%s</span>", yytext);
+}
+
+"<<="|"++"|"="|":="|","|";"|"%"|">>"|"%="|">>="|"--"|"!"|"..."|"."|":"|"&^"|"&^=" {
+ printf("<span class=\"operator\">%s</span>", yytext);
+}
+
+
+[;(){}? \t\n]+ printf("%s", yytext); /* echo the rest */
+
+. printf( "Unrecognized character: %s\n", yytext );
+
+%%
+
+int yywrap()
+{
+ return 1;
+}
+
+void printStyles() {
+ puts(
+ " <style>"
+ " .identifier {"
+ " color: brown;"
+ " }"
+ " .keyword {"
+ " color: #1fbfe2;"
+ " }"
+ " .number {"
+ " color: blue;"
+ " }"
+ " .string {"
+ " color: green;"
+ " }"
+ " .operator {"
+ " color: #db184c;"
+ " font-style: bold;"
+ " }"
+ " </style>"
+ );
+}
+
+void printHtmlHeader() {
+ puts(
+ "<head>"
+ " <title>hello_world.go</title>"
+ );
+ printStyles();
+ puts(
+ "</head>"
+ );
+}
+
+void printBody() {
+ puts(
+ "<body bgcolor=\"#e2cdb7\">"
+ " <pre class=\"code\">"
+ );
+ yylex();
+ puts("</pre></body>");
+}
+
+
+void printHTML() {
+ puts(
+ "<!doctype>"
+ "<html>"
+ );
+ printHtmlHeader();
+ printBody();
+ puts("</html>");
+}
+
+int main(int argc, const char* argv[])
+{
+ ++argv, --argc; /* skip over program name */
+ if ( argc > 0 )
+ yyin = fopen( argv[0], "r" );
+ else
+ yyin = stdin;
+
+ printHTML();
+ return 0;
+}
diff --git a/homework/81319/golang2html/hello_world.go b/homework/81319/golang2html/hello_world.go
new file mode 100644
index 0000000..0a31497
--- /dev/null
+++ b/homework/81319/golang2html/hello_world.go
@@ -0,0 +1,21 @@
+package main
+
+import (
+ "fmt"
+)
+
+type HelloWorlder struct {
+ message string
+}
+
+func (hw *HelloWorlder) greet() {
+ fmt.Println(hw.message)
+}
+
+func main() {
+ helloWorlder := &HelloWorlder{
+ message: "Hello, world!",
+ }
+
+ helloWorlder.greet()
+} \ No newline at end of file