Posts

Showing posts with the label Parser

Playing with xml

XML is a convenient way to define external DSL. On one of my previous posts  ( Filtering using Scala Parser Combinators ) I used parser combinators  JavaTokenParsers with PackratParsers  This time I will use different approach using XML . XML is easy to handle by non-developers and easy to read. We can query XML using XPATH like expressions and using pattern matching. In this post we will define some business rules using XML and we will treat our XML as first class citizen. Source code can be found  here . let's define our DSL : Our rules will use AND/OR operators but to make it more interesting, our operators  will not use  the following pattern: <expression><operator><expression>  (where expression evaluates to boolean). because XML have opening and closing tags we can define something like : <operator><expression1><expression2>...<expression N></operator> now we can define our AND/OR o...

Filtering using Scala Parser Combinators

Filtering using Scala Parser Combinators So I got interesting task to enable record filtering according to some rules that we get from configuration represented as text. well so far we know two things : 1. we need a filter that accepts a record as an input and might return a record as its output : def filter ( record : Record ) : Option [ Record ] 2. we need to get a text with some kind of rule e.g  "foo = 'FOO'" , "bar = 8", "buz IS NOT NULL "  and AND / OR combinations . where foo/bar/buz are record's attributes * well I know that "null" is a dirty word in FP but that was the requirement since it was a mix-in with java project. Following  the rule of three  so is our todo list : parse the text evaluate it's Boolean expression in a form of Abstract Syntax Tree. filter according the Boolean evaluation. The complete Source Code can be found  here  (you are more than welcome to contribute ) Parser Combin...