From 15d4746a4ec11bd92945fd7097361ccdee4684a6 Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Wed, 10 Oct 2018 02:04:07 +0300 Subject: added JavaScript flex based highlighter --- demo/flex/js2html.flex | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 demo/flex/js2html.flex (limited to 'demo/flex/js2html.flex') diff --git a/demo/flex/js2html.flex b/demo/flex/js2html.flex new file mode 100644 index 0000000..738895e --- /dev/null +++ b/demo/flex/js2html.flex @@ -0,0 +1,76 @@ +/* scanner for a toy JavaScript-like language */ + +%{ +%} + +DIGIT [0-9] +ID [a-z_$][a-z0-9_$]* +STRQ '([^\']|(\\\'))*' +STRDQ "([^\"]|(\\\"))*" + +%% + +{DIGIT}+ { + printf("%s", yytext); + } + +{DIGIT}+"."{DIGIT}* { + printf("%s", yytext); + } + +if|else|for|while|do|break|switch|case|function|return|new|delete|try|throw|catch|class|public|private { + printf("%s", yytext); +} + +{ID} printf("%s", yytext); +{STRQ}|{STRDQ} printf("%s", yytext); + +"."|"+"|"-"|"*"|"/"|"="|"!"|">"|"<"|"!=="|"==="|">="|"<="|"++"|"--"|"=>" printf("%s", yytext); + +[;(){}:? \t\n]+ printf("%s", yytext); /* echo the rest */ + +. printf( "Unrecognized character: %s\n", yytext ); + +%% + +int yywrap() +{ + return 1; +} + + +int main(int argc, const char* argv[]) +{ + ++argv, --argc; /* skip over program name */ + if ( argc > 0 ) + yyin = fopen( argv[0], "r" ); + else + yyin = stdin; + + puts( + "" + "" + "" + " hello.js" + " " + "" + "" + "
"
+        );
+    yylex();
+    puts("
"); + return 0; +} -- cgit v1.3.1-26-gb76a