Typed Parsing and Unparsing for Untyped Regular Expression Engines

Typed Parsing and Unparsing for Untyped Regular Expression Engines

Typed Parsing and Unparsing for Untyped Regular Expression Engines Gabriel Radanne University of Freiburg Germany [email protected] Abstract ACM Reference Format: Regular expressions are used for a wide variety of purposes Gabriel Radanne. 2019. Typed Parsing and Unparsing for Untyped from web-page input validation to log file crawling. Very Regular Expression Engines. In Proceedings of the 2019 ACM SIG- PLAN Workshop on Partial Evaluation and Program Manipulation often, they are used not only to match strings, but also to (PEPM ’19), January 14–15, 2019, Cascais, Portugal. ACM, New York, extract data from them. Unfortunately, most regular expres- NY, USA, 12 pages. https://doi.org/10.1145/3294032.3294082 sion engines only return a list of the substrings captured by the regular expression. The data has to be extracted from the matched substrings to be validated and transformed manu- ally into a more structured format. 1 Introduction For richer classes of grammars like CFGs, such issues can Regular expressions are used in a wide variety of contexts, be solved using type-indexed combinators. Most combinator from checking inputs of web forms to extracting data con- libraries provide a monadic API to track the type returned tained in text files. In many cases, the goal of the regular by the parser through easy-to-use combinators. This allows expression is not only to match a given text, but also to ex- users to transform the input into a custom data-structure tract information from it through capture groups. Capture and go through complex validations as they describe their groups are annotations on parts of the regular expression grammar. that indicates that the substring matched by these subex- In this paper, we present the Tyre library which provides pressions should be returned by the matching engine. For type-indexed combinators for regular languages. Our com- instance, "([a-zA-Z]*):([0-9]*)" matches strings of the binators provide type-safe extraction while delegating the form "myid:42" and captures the part before and after the task of substring matching to a preexisting regular expres- colon. sion engine. To do this, we use a two layer approach where Extraction using capture groups, however, only returns the typed layer sits on top of an untyped layer. This tech- strings. In the regex above, the semantics we assign to the nique is also amenable to several extensions, such as routing, second group is clearly the one of a number, but this infor- unparsing and static generation of the extraction code. We mation is not provided to the regular expression engine, and also provide a syntax extension, which recovers the familiar it is up to the programmer to parse the stream of digits into a and compact syntax of regular expressions. We implemented proper integer. This problem only grows more complex with this technique in a very concise manner and evaluated its the complexity of the data to extract. For instance, if we were usefulness on two practical examples. to consider parsing URIs with regular expressions, we would need to extract a complex structure with multiple fields, lists CCS Concepts • Theory of computation → Regular and alternatives. Even static typing does not help here, since languages; Parsing; • Software and its engineering → all the captured fields are strings! This problem, often de- Functional languages; scribed as input validation, can not only be the source of bugs but also serious security vulnerabilities. Keywords Functional programming, Static typing, Regular One approach that might help is to rely on “full regular ex- expressions, unparsing, OCaml pression parsing” which consists in returning the parsetree of a string matched by a given regular expression. This is gen- PEPM ’19, January 14–15, 2019, Cascais, Portugal erally coupled with an interpretation of regular expressions © 2019 Copyright held by the owner/author(s). Publication rights licensed as types to determine the shape of the resulting parsetree: a to ACM. concatenation of regular expression is interpreted as a prod- This is the author’s version of the work. It is posted here for your personal uct type, an alternative as a sum type and the Kleene star as use. Not for redistribution. The definitive Version of Record was published a list, thus forming a small algebra of types that reflects the in Proceedings of the 2019 ACM SIGPLAN Workshop on Partial Evaluation and compositional properties of regular expressions. Program Manipulation (PEPM ’19), January 14–15, 2019, Cascais, Portugal, https://doi.org/10.1145/3294032.3294082. Full regular expression parsing is not completely sufficient: indeed we want both the ability to use structured types, but PEPM ’19, January 14–15, 2019, Cascais, Portugal Gabriel Radanne also to transform and verify the input. We also want to ig- nore the semantically-irrelevant parts of the string. A final 1 open Tyre problem is that full regular expression parsing is a very rare 2 3 let name : string Tyre.t = [%tyre"[[:alnum:]␣]+"] feature among regex engines. Mature implementations such 4 as Re2 [Cox 2010], PCRE [Hazel 2015], Javascript regular 5 let host : < domain:string ; port:int option > Tyre.t = expressions [ECMAScript 2018] or Hyperscan [Intel 2018] 6 [%tyre"(?<domain>[^/:?#]+)(:(?<port>(?&int)))?"] do not provide it. While writing a new regular expression 7 engine might seem tempting, writing a featureful, efficient 8 let api = function%tyre and portable engine that supports features such as online 9 |"(?&h:host)/name/(?&species:name)" determinization, lookarounds operators and both POSIX and 10 -> h, ‘Host species greedy semantics is a significant undertaking, as demon- 11 |"(?&h:host)/hump/(?&hump:int)(\?extinct=(?&b:bool))?" strated by the projects listed above. 12 -> h, ‘Hump (hump, b) In this paper, we present a technique to provide type-safe extraction based on the typed interpretation of regular ex- Figure 1. HTTP endpoints for a website to classify camlids. pressions while delegating the actual matching to an external regular expression engine that only supports substring ex- traction. This provides users with the convenience and type- that have two humps through the URL camelidae.ml/humps/ safety of parsing combinators, while using a mature regular 2. Finally, we want the ability to filter extinct species by expression engine. Our technique relies on two-layer regular adding ?extinct=true to the URL. Writing such API with nor- expressions where the upper layer allows to composes and mal regular expressions is often delicate since it not only transforms data in a well-typed way, while the lower layer needs to match the URL, but also to extract and convert parts is simply composed of untyped regular expressions that can of it. In the rest of this section, we assume basic familiarity leverage features from the underlying engine. This two-layer with OCaml. approach is also compatible with routing, unparsing, and Figure 1 implements the routing for this REST API with either online of offline compilation of regular expressions. typed regular expressions with automatic extraction. It uses We implemented our technique in the OCaml Tyre package, an extended syntax similar to Perl Compatible Regular Ex- along with a syntax extension that allows the use of a syn- pressions. We first define two independent typed regular tax similar to PCREs. While our main implementation relies expressions, name (Line3) and host (Line5), we then define on ocaml-re, an optimized and mature regular expression our two URL endpoints and name the resulting route in api library for OCaml, we also implemented it as an OCaml func- (Line8). For ease of understanding, we added some type tor (i.e., a function over modules) that is independent of the annotations, but they are not necessary. engine. The syntax [%tyre"..."] specifies a typed regular ex- Our main contribution is the design and the implemen- pression using the PCRE syntax. The name typed regular tation of Tyre, including a typed transformation technique expression defined Line3 is in fact a normal regular expres- which can be used to decompose such input validation prob- sion which recognizes species names made of a non-empty lems into an untyped parsing step which can be delegated succession of alphanumerical characters and spaces. The to preexisting engines, and a validation step. The rest of the resulting value is of type string Tyre.t which represents paper is structured as followed. We first introduce typed a typed regular expression Tyre.t which captures data of regular expressions through real world code examples that type string. uses Tyre in Section2. We then describe our technique in The host regular expression defined Line5 matches (sim- Sections3 and4. We evaluate the expressivity and the perfor- plified) host names composed of a domain and an optional mance claims in Section5. Finally, we compare with existing port using the syntax "foo.com:123". To capture these two approaches in Section6 and conclude. parts, it uses two named capture groups for the domain and the port, using the syntax "(?<name>re)". Since we have 2 The Tyre library two named capture groups, the regex host captures a do- main and a port. The type indeed shows we capture an object Tyre has been used in real-world applications for parsing, with two fields. The domain is represented by the regular printing and routing complex regular-expression-based for- expression [^/:?#]+ and thus captured as a string. The port mats such as logs and URLs. To introduce Tyre, we use the ex- is represented by (?&int) which uses another regular ex- ample of a simple website that classifies species of the Camel- pression previous declared, in this case Tyre.int of type idae family. Regular expressions match URLs and define a int Tyre.t capturing an integer. Since the port is optional, REST API for our Camelidae classifiers.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us