Text parsers
Parsing is something that web apps need to do quite often. Opa features a built-in syntax for building text parsers, which are first class values just as functions. The parser is based on parsing expression grammar (http://en.wikipedia.org/wiki/Parsing_expression_grammar), which may look like regular expressions at first, but do not behave anything like them. One big advantage of text parsers over regular expressions is that you can easily combine parsers. A good example is parsing URLs. Let's start right away with our first Opa parser:
first_parser = parser { case "Opa" : 1 }
For first_parser
, the expressions are just literal strings, which means this parser will succeed only if fed with the string "Opa"
. Then how to use this parser? The module Parser
(http://doc.opalang.org/module/stdlib.core.parser/Parser) has a bunch of functions to deal with parsers. The most important one is:
Parser.try_parse : Parser.general_parser('a), string -> option('a)
It takes a parser and a string...