Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions homework/81906/hs2html.v2/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spaghetti.tree е синтактичното дърво на spaghetti.hs, генерирано от tree-sitter
([1]) и неговата граматика за haskell([2]).
[1]: https://github.com/tree-sitter/tree-sitter
[2]: https://github.com/tree-sitter/tree-sitter-haskell
140 changes: 140 additions & 0 deletions homework/81906/hs2html.v2/hs2html.flex
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* scanner for Haskell */

%{
#define YY_NO_UNISTD_H
%}

DIGIT [0-9]
ID [a-z_]+[A-Za-z0-9_$]*
CLASSNAME [A-Za-z0-9]*
COMMENT --[\(\)\[\]\{\} \w]*\n
STRQ '([^\']|(\\\'))*'
STRDQ \"([^\"]|(\\\"))*\"
SFOLD \/\*\{[0-9]+\}|\/\*
FFOLD \*\/

%%

{DIGIT}+ {
printf("<span class=\"number\">%s</span>", yytext);
}

{DIGIT}+"."{DIGIT}* {
printf("<span class=\"number\">%s</span>", yytext);
}

as|case|of|class|data|family|instance|default|deriving|instance|do|forall|foreign|hiding|if|then|else|import|infix|infixl|infixr|instance|let|in|mdo|module|newtype|proc|qualified|rec|type|family|instance|where {
printf("<span class=\"keyword\">%s</span>", yytext);
}


{ID} printf("<span class=\"identifier\">%s</span>", yytext);
{CLASSNAME} printf("<span class=\"classname\">%s</span>", yytext);
{STRQ}|{STRDQ} printf("<span class=\"string\">%s</span>", yytext);
{COMMENT} printf("<span class=\"comment\">%s</span>", yytext);

"=" printf("<span class=\"operator\">%s</span>", yytext);
"!" printf("<span class=\"operator\">%s</span>", yytext);
"-" printf("<span class=\"operator\">%s</span>", yytext);
"+" printf("<span class=\"operator\">%s</span>", yytext);
"/" printf("<span class=\"operator\">%s</span>", yytext);
"?" printf("<span class=\"operator\">%s</span>", yytext);
"." printf("<span class=\"operator\">%s</span>", yytext);
">" printf("<span class=\"operator\">%s</span>", yytext);
"#" printf("<span class=\"operator\">%s</span>", yytext);
"*" printf("<span class=\"operator\">%s</span>", yytext);
"@" printf("<span class=\"operator\">%s</span>", yytext);
"\\" printf("<span class=\"operator\">%s</span>", yytext);
"`" printf("<span class=\"operator\">%s</span>", yytext);
"|" printf("<span class=\"operator\">%s</span>", yytext);
"~" printf("<span class=\"operator\">%s</span>", yytext);
"&" printf("<span class=\"operator\">%s</span>", yytext);
"-<" printf("<span class=\"operator\">%s</span>", yytext);
"->" printf("<span class=\"operator\">%s</span>", yytext);
"::" printf("<span class=\"operator\">%s</span>", yytext);
"<-" printf("<span class=\"operator\">%s</span>", yytext);
"=>" printf("<span class=\"operator\">%s</span>", yytext);
"/=" printf("<span class=\"operator\">%s</span>", yytext);
"-<<" printf("<span class=\"operator\">%s</span>", yytext);

{SFOLD} {
printf("<button class=\"toggle-hide\" onclick=\"toggle(this)\">+</button><span class=\"hidable\">");
}
{FFOLD} printf("</span>");

[;():?,\[\] \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(
"<!doctype>"
"<html>"
"<head>"
" <title>spaghetti.hs</title>"
" <script>"
" function toggle(btn){"
" let span = btn.nextSibling;"
" if(span.style.display == \"none\")"
" span.style.display = \"inline\";"
" else"
" span.style.display = \"none\";"
" }"
" </script>"
" <style>"
" body {"
" background-color: #2e3436;"
" color: white;"
" }"
" button {"
" border-radius: 0px;"
" border-width: 0px;"
" height: 15px;"
" width: 15px;"
" }"
" .keyword {"
" color: #b4fa70;"
" }"
" .number {"
" color: blue;"
" }"
" .string {"
" color: #e9b96e;"
" }"
" .operator {"
" font-style: bold;"
" color: #fcaf3e;"
" }"
" .identifier {"
" color: white;"
" }"
" .classname {"
" color: lightblue;"
" }"
" .comment {"
" color: #6ec619;"
" }"
" </style>"
"</head>"
"<body>"
" <pre class=\"code\">"
);
yylex();
puts("</pre></body></html>");
return 0;
}
Binary file added homework/81906/hs2html.v2/lib/libtree-sitter.a
Binary file not shown.
Binary file added homework/81906/hs2html.v2/lib/libtree-sitter.so
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions homework/81906/hs2html.v2/magic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
grep rhs spaghetti.tree | sed 's/^[ ]*//' | sed 's/rhs: (//' | tr -d [,]- | tr -s ' ' | awk -F' ' '{ if ( $1 == "exp_do" || $1 == "exp_cond" || $1 == "exp_let_in") print($2, $3, $4, $5) }' > coordinatesOfBlocks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use treesitter API?

./markBlocks
flex hs2html.flex
gcc lex.yy.c
./a.out spaghetti.hs-foldable > spaghetti.html
rm lex.yy.c a.out coordinatesOfBlocks spaghetti.hs-foldable
Binary file added homework/81906/hs2html.v2/markBlocks
Binary file not shown.
31 changes: 31 additions & 0 deletions homework/81906/hs2html.v2/markBlocks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream cfile("coordinatesOfBlocks");
ifstream hsfile("spaghetti.hs");
ofstream result("spaghetti.hs-foldable");

int lineN = 0;
while(!cfile.eof()) {
int startLine, startColumn, endLine, endColumn;
cfile >> startLine >> startColumn >> endLine >> endColumn;

string line;
while(lineN < endLine && !hsfile.eof()){
getline(hsfile, line);
if(lineN == startLine){
line.insert(startColumn, "/*");
}
result << line << endl;;
lineN++;
}
getline(hsfile, line);
if(lineN == endLine){
line.append("*/");
}
lineN++;
result << line << endl;
}
return 0;
}
Loading