Rascal is currently hosting my simple web server. Here I have an input for users using HTML textarea tag, and a submit button. However, I can't figure out how to request that input data from users when they have submitted it. I also don't see much documentation about it, so any help would be appreciated!
1 Answers
Assuming you either use the Content
and/or util::Webserver
from the library for serving content from Rascal, you always provide a function of type Response (Request)
to the server. This function does everything from serving index.html
to receiving form inputs, and handling XMLHttpRequests. All you have to do is write the function's alternatives.
The kinds of Requests you can get are defined like this in Content.rsc
:
data Request (map[str, str] headers = (), map[str, str] parameters = (), map[str,str] uploads = ())
= get (str path)
| put (str path, Body content)
| post(str path, Body content)
| delete(str path)
| head(str path)
;
And responses are defined by:
data Response
= response(Status status, str mimeType, map[str,str] header, str content)
| fileResponse(loc file, str mimeType, map[str,str] header)
| jsonResponse(Status status, map[str,str] header, value val, bool implicitConstructors = true, bool implicitNodes = true, str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ss\'Z\'")
;
In the example below I use convenience utility functions such as Content::response(str)
which wrap an html string with the right HTTP status and mimetypes.
Example:
// this serves the initial form
Response myServer(get("/"))
= response("\<p\>What is your name?\</p\>
'\<form action=\"/submit\" method=\"GET\"\>
' \<input type=\"text\" name=\"name\" value=\"\"\>
'\</form\>
");
// // this responds to the form submission, now using a function body with a return (just for fun):
Response myServer(p:get("/submit")) {
return response("Hello <p.parameters["name"]>!");
}
// in case of failing to handle a request, we dump the request back for debugging purposes:
default Response myServer(Request q) = response("<q>");
Now we can serve this directly from the REPL. The content will show up in an Eclipse editor window or in your default browser and will stay available for 30 minutes after the last interaction in Rascal's internal application server:
rascal>content("test", myServer)
Serving 'test' at |http://localhost:9050/|
Or we can serve it on our own, then browse to http://localhost:10001
to test the server. We have to shut the thing down manually when we're done:
rascal>import util::Webserver;
ok
rascal>serve(|http://localhost:10001|, myServer)
ok
rascal>shutdown(|http://localhost:10001|)

- 6,393
- 1
- 15
- 26
-
Thank you so much, this was of great help! Especially the examples, I appreciate that you added them and explained. – michael Dec 11 '20 at 15:02
-
Also I faced another problem, while working with this. One of the inputs from users is a grammar (for the parse tree I am visualising). What I currently do is that I save that grammar-input with writeFile() in a new rascal module, and then I have to shutdown the server and restart the server up again for it to include the new rascal module with the new grammar. This is not very optimal, for obvious reasons. So my question is: is there a better way to start using the new grammar the user put in somehow, without restarting the server? – michael Dec 12 '20 at 23:22
-
Sure! You can use the modules in Lang::Rascal::grammar:: definition and the Rascal syntax to parse the module and then transform the grammar in it to a reified type that includes the grammar in abstract form. That's quite easy to do. I use it for my disambiguation and grammar testing user interface all the time. The dynamically created reified type can be used to parse again. Rascal will cache (weak referenced) the generated parser based on a hash of the grammar. – Jurgen Vinju Dec 13 '20 at 10:06
-
Thank you! I did not know that. Do you just import lang::rascal::grammar::definition::Modules; and then use the modules2grammar(str main, set[Module] modules)? I am also not sure what that first argument is supposed to be. Are there any documentations about this? I only see the github repository mentioning something about this. – michael Dec 13 '20 at 16:21
-
1No documentation; you'll have to read the code. I'll try and find some example usage code for you tomorrow – Jurgen Vinju Dec 13 '20 at 16:22
-
Thank you I really appreciate that! – michael Dec 13 '20 at 16:35
-
1@michael have a look here: https://github.com/cwi-swat/drambiguity/blob/master/src/GrammarEditor.rsc – Jurgen Vinju Dec 14 '20 at 09:04