Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Lexer =
- open FParsec.Primitives
- open FParsec.CharParsers
- let escapeCharacters<'a> : Parser<string, 'a> =
- ["+"; "-"; "&&"; "||"; "!"; "("; ")"; "{"; "}"; "["; "]"; "^"; """; "~"; "*"; "?"; ":"; "\\"] |> Seq.map pstring |> Seq.reduce (<|>)
- let wordOperator<'a> : Parser<Token, 'a> =
- pipe3 spaces1 (pstring "TO" <|> pstring "OR" <|> pstring "AND" <|> pstring "NOT") spaces1 (fun _ s _ -> Operator s)
- let operator<'a> : Parser<Token, 'a> =
- (escapeCharacters |>> Operator) <|> attempt wordOperator <|> (spaces1 |>> fun c -> Operator "OR")
- let term<'a> : Parser<Token, 'a> =
- many1Chars (notFollowedBy operator >>. anyChar) |>> Term
- let phrase<'a> : Parser<Token, 'a> =
- between (pchar '\"') (pchar '\"') (manyStrings ((notFollowedBy operator >>. anyString 1) <|> (skipString "\\" >>. escapeCharacters))) |>> Phrase
- let exec<'a> : Parser<list<Token>, 'a> =
- manyTill (term <|> phrase <|> operator) eof
- let tokenize input =
- let r = runParserOnString exec "" "" input
- match r with
- | Success (a, b, c) ->
- a
- | Failure (a, b, c) ->
- failwith a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement