From a4c91889a67ec12057d08d21a81a1f4ed677fc67 Mon Sep 17 00:00:00 2001 From: Georgi Stefanov Date: Thu, 25 Oct 2018 12:54:32 +0300 Subject: 81319 golang2html lexer (#20) * Add highlighter for golang * Add readme with instructions * Fix readme --- homework/81319/golang2html/README.md | 30 +++++++ homework/81319/golang2html/golang2html.flex | 121 ++++++++++++++++++++++++++++ homework/81319/golang2html/hello_world.go | 21 +++++ 3 files changed, 172 insertions(+) create mode 100644 homework/81319/golang2html/README.md create mode 100644 homework/81319/golang2html/golang2html.flex create mode 100644 homework/81319/golang2html/hello_world.go 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("%s", yytext); + } + +{DIGIT}+"."{DIGIT}* { + printf("%s", yytext); + } + +break|default|func|interface|select|case|defer|go|map|struct { + printf("%s", yytext); +} + +chan|else|goto|package|switch|const|fallthrough|if|range|type|continue|for|import|return|var { + printf("%s", yytext); +} + +{ID} printf("%s", yytext); +{STRQ}|{STRDQ} printf("%s", yytext); + +"+"|"&"|"+="|"&="|"&&"|"=="|"!="|"-"|"|"|"-="|"|="|"||"|"<" { + printf("%s", yytext); +} + +"<="|"["|"]"|"*"|"^"|"*="|"^="|"<-"|">"|">="|"{"|"}"|"/"|"<<"|"/=" { + printf("%s", yytext); +} + +"<<="|"++"|"="|":="|","|";"|"%"|">>"|"%="|">>="|"--"|"!"|"..."|"."|":"|"&^"|"&^=" { + printf("%s", yytext); +} + + +[;(){}? \t\n]+ printf("%s", yytext); /* echo the rest */ + +. printf( "Unrecognized character: %s\n", yytext ); + +%% + +int yywrap() +{ + return 1; +} + +void printStyles() { + puts( + " " + ); +} + +void printHtmlHeader() { + puts( + "" + " hello_world.go" + ); + printStyles(); + puts( + "" + ); +} + +void printBody() { + puts( + "" + "
"
+      );
+      yylex();
+      puts("
"); +} + + +void printHTML() { + puts( + "" + "" + ); + printHtmlHeader(); + printBody(); + puts(""); +} + +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 -- cgit v1.3.1-26-gb76a