parser-combinators

Ignoring an arbitrary prefix in a parser combinator

≡放荡痞女 提交于 2019-12-24 11:15:59
问题 After getting fed up with regexes I have been trying to use scala's parser combinator libraries as a more intuitive replacement for regexes. However, I've run into a problem when I want to search a string for a pattern and ignore things that come before it, for example if I want to check if a string contains the word "octopus" I can do something like val r = "octopus".r r.findFirstIn("www.octopus.com") Which correctly gives Some(octopus) . However, using parser combinators import scala.util

Recursive parsing based on level numbers

僤鯓⒐⒋嵵緔 提交于 2019-12-23 16:11:29
问题 I have a tricky question (at least in my perspective), regarding Scalas parser combinators and recursive parsing. I currently building a small parser which should be able to parse PL/1 structures like this: dcl 1 data, 3 subData, 5 tmp char(15), 5 tmp1 char(15), 3 subData2, 5 tmp2 char(10), 5 tmp3 char(5); In this scenario I want to build a AST as follows: Record(data) -> (Record(subData),Record(subData2)) Record(subData) -> (Char(tmp),Char(tmp1)) Record(subData2) -> (Char(tmp2),Char(tmp3))

How to further improve error messages in Scala parser-combinator based parsers?

江枫思渺然 提交于 2019-12-22 05:07:00
问题 I've coded a parser based on Scala parser combinators: class SxmlParser extends RegexParsers with ImplicitConversions with PackratParsers { [...] lazy val document: PackratParser[AstNodeDocument] = ((procinst | element | comment | cdata | whitespace | text)*) ^^ { AstNodeDocument(_) } [...] } object SxmlParser { def parse(text: String): AstNodeDocument = { var ast = AstNodeDocument() val parser = new SxmlParser() val result = parser.parseAll(parser.document, new CharArrayReader(text.toArray))

How to combine parsers with different types of Elem

我是研究僧i 提交于 2019-12-21 17:49:57
问题 I'm trying to create a parser that combines Regex parsers and a custom parser I have. I've looked at Scala: How to combine parser combinators from different objects, but that question and answers deal with parsers that have the same type of Elem . Say I have a few RegexParsers, and also a parser that does a lookup for a String: trait NumbersParsers extends RegexParsers { def number = """\d+""".r } trait LookupParsers extends Parsers { type Elem = String def word = elem("word", (potential

Accessing Scala Parser regular expression match data

守給你的承諾、 提交于 2019-12-20 19:39:04
问题 I wondering if it's possible to get the MatchData generated from the matching regular expression in the grammar below. object DateParser extends JavaTokenParsers { .... val dateLiteral = """(\d{4}[-/])?(\d\d[-/])?(\d\d)""".r ^^ { ... get MatchData } } One option of course is to perform the match again inside the block, but since the RegexParser has already performed the match I'm hoping that it passes the MatchData to the block, or stores it? 回答1: Here is the implicit definition that converts

Ignoring C-style comments in a Scala combinator parser

孤人 提交于 2019-12-20 12:38:44
问题 What is the most simple way to make my parser respect (ignore) C-style comments. I'm interested in both comment types, though a solution for only one type is also welcome. I'm currently simply extending JavaTokenParsers. 回答1: You can use a simple regular expression, as long as you don't nest comments. Put it inside whiteSpace : scala> object T extends JavaTokenParsers { | protected override val whiteSpace = """(\s|//.*|(?m)/\*(\*(?!/)|[^*])*\*/)+""".r | def simpleParser = ident+ | } defined

Using Positional and positioned() in scala parser combinators

最后都变了- 提交于 2019-12-13 02:49:55
问题 With separate Lexer and Parser ... class YamlLexical extends StdLexical with YamlTokens with RegexParsers {... object YamlParser extends StdTokenParsers with YamlTokens with PackratParsers {... ... how to get the position of the parsed string into AST classes? (... positioned(elem(...)) * ... )^^ { ... => List( Ast(startpos, parsedtext, ... subnodes ... ), ... )} 回答1: The type of positioned is def positioned[T <: Positional](p: ⇒ Parser[T]): Parser[T] which means that the parsed elements must

Scala PackratParsers does not backtrack as it should?

ぃ、小莉子 提交于 2019-12-13 00:35:10
问题 I have the following code for simple parser of logical expressions: import scala.util.parsing.combinator.RegexParsers import scala.util.parsing.combinator.PackratParsers object Parsers extends RegexParsers with PackratParsers // Entities definition sealed trait LogicalUnit case class Variable(name: String) extends LogicalUnit case class Not(arg: LogicalUnit) extends LogicalUnit case class And(arg1: LogicalUnit, arg2: LogicalUnit) extends LogicalUnit import Parsers._ // In order of descending

Reuse parser within another parser with Scala parser combinators

感情迁移 提交于 2019-12-12 19:04:12
问题 I have a parser for arithmetic expressions: object FormulaParser extends JavaTokenParsers { def apply(input: String) = parseAll(formula, input) // ... val formula: Parser[Formula] = comparison | comparable | concatenable | term | factor } I need to parse a different language that can contain formulas. Let's say I need to parse something like X < formula . Unfortunately I cannot reuse FormulaParser.formula in my new parser: object ConditionParser extends JavaTokenParsers { def apply(input:

How to ignore single line comments in a parser-combinator

血红的双手。 提交于 2019-12-12 11:01:06
问题 I have a working parser, but I've just realised I do not cater for comments. In the DSL I am parsing, comments start with a ; character. If a ; is encountered, the rest of the line is ignored (not all of it however, unless the first character is ; ). I am extending RegexParsers for my parser and ignoring whitespace (the default way), so I am losing the new line characters anyway. I don't wish to modify each and every parser I have to cater for the possibility of comments either, because