I’m currently building a web-server which can receive request, and send back a response.
I’ve managed to embed a port of Google’s v8 JavaScript engine to c# (javascript.net) to my project and I want to parse a requested file and run the server-sided JavaScript code that in it. I decided that this code will be contained inside a 2-character brackets, <: for opening and :> for closing. I started to parse it with code I written but after encountering some problems which made the code more messy and probably not very efficient I decided to go ahead and try using RegEx (I had you study it because I’ve never used it before). BUT WAIT. After talking to my friend about it he send me this post RegEx match open tags except XHTML self-contained tags I understood that it isn’t a good idea…
So my question is, How do I parse such thing? (Taking efficiency and clean code into account, after all it’s a webserver).
Thanks in advance!
I’m currently building a web-server which can receive request, and send back a response.
Share
Ideally, what you’d want to do is hook into V8’s lexer so you don’t end up catching things inside of strings and such. I looked at the source to that .NET wrapper, however, and it looks like it doesn’t allow that much customization. Instead, you may want to create a small state machine. You’d probably want at least these states:
<:and:>tags)<and are waiting for a potential:)<:and:>tags):and are unsure whether a>or something else is next)It may not be so quick to write as a regular expression, but it would be able to handle code like this:
1On second thought, it’s probably not so easy to detect regular expressions.