官术网_书友最值得收藏!

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 as parameters and produces an optional value of some type. Let's see how to use this function:

x = Parser.try_parse(parser1,"Opa")  //x = {some: 1}
y = Parser.try_parse(parser1,"Java") //y = {none}

Now let's consider the following parsers:

digit1 = parser { case x=[0-9]+: x }
digit2 = parser { case x=([0-9]+): x }

Both digit1 and digit2 accept a number string like "5","100", and both will assign the value to the identifier x. If we feed the parser digit1 with the string "100", x will be the parsing result of the string: a list of characters ['1','0','0']. If we feed the string "100" to parser digit2, x will be the input string: 100. So, if we want to get hold of the input string, we need to put the expression in parentheses.

Let's move it a little further; consider the following parser:

abs_parser = parser{
  case x=("+"?[0-9]+): Int.of_string("{x}")
  case x=("-"[0-9]+) : 0 – Int.of_string("{x}")
}
x = Parser.try_parse(abs_parser,"-100") // x = {some: 100}

This parser accepts an integer string and returns the absolute value. You may figure out how it works with the previous knowledge. Note that even if the expression of PEG looks like a regular expression, they are different.

主站蜘蛛池模板: 阿克陶县| 洛阳市| 永仁县| 遂川县| 盐亭县| 苍山县| 永仁县| 贵德县| 军事| 察隅县| 若羌县| 高唐县| 虎林市| 塔河县| 昌都县| 延津县| 阳新县| 宁明县| 湖口县| 卓尼县| 铜陵市| 焦作市| 牟定县| 巩留县| 鄂伦春自治旗| 万荣县| 上虞市| 乐平市| 屏南县| 泾源县| 房产| 德庆县| 洞头县| 榆中县| 诏安县| 瓮安县| 宁安市| 兰州市| 嘉善县| 黄龙县| 石楼县|