{-# LANGUAGE PackageImports #-}
module Graphics.Implicit.ExtOpenScad.Parser.Lexer (whiteSpace, matchTrue, matchFalse, matchFunction, matchInclude, matchUse, matchIf, matchElse, matchModule, matchLet, matchUndef, matchTok, matchColon, matchSemi, matchComma, matchIdentifier, surroundedBy, matchLT, matchLE, matchGT, matchGE, matchEQ, matchNE, matchCAT, matchOR, matchAND, matchEach, lexer) where
import Prelude (String, Char, Bool(True), (>>), pure)
import "monads-tf" Control.Monad.Identity (Identity)
import Text.Parsec.String (GenParser)
import qualified Text.Parsec.Token as P (whiteSpace, reserved, identifier, reservedOp)
import Text.Parsec.Language (GenLanguageDef, emptyDef)
import Text.Parsec.Token (GenTokenParser, makeTokenParser, commentStart, commentEnd, commentLine, nestedComments, caseSensitive, colon, semi, comma, identStart, identLetter, reservedNames, reservedOpNames)
import Text.Parsec (char, between)
import Text.Parsec.Char (noneOf)
openScadStyle :: GenLanguageDef String u0 Identity
openScadStyle
= emptyDef
{ commentStart = "/*"
, commentEnd = "*/"
, commentLine = "//"
, nestedComments = True
, identStart = noneOf " ,|[]{}()+-*&^%#@!~`'\"\\/;:.,<>?=1234567890"
, identLetter = noneOf " ,|[]{}()+-*&^%#@!~`'\"\\/;:.,<>?="
, reservedNames = ["module", "function", "if", "else", "let", "each", "true", "false", "undef", "include", "use"]
, reservedOpNames= ["<=", ">=", "==", "!=", "&&", "||"]
, caseSensitive = True
}
lexer :: GenTokenParser String st Identity
lexer = makeTokenParser openScadStyle
whiteSpace :: GenParser Char st ()
whiteSpace = P.whiteSpace lexer
matchTrue :: GenParser Char st ()
matchTrue = P.reserved lexer "true"
matchFalse :: GenParser Char st ()
matchFalse = P.reserved lexer "false"
matchFunction :: GenParser Char st ()
matchFunction = P.reserved lexer "function"
matchInclude :: GenParser Char st ()
matchInclude = P.reserved lexer "include"
matchUse :: GenParser Char st ()
matchUse = P.reserved lexer "use"
matchIf :: GenParser Char st ()
matchIf = P.reserved lexer "if"
matchElse :: GenParser Char st ()
matchElse = P.reserved lexer "else"
matchModule :: GenParser Char st ()
matchModule = P.reserved lexer "module"
matchLet :: GenParser Char st ()
matchLet = P.reserved lexer "let"
matchUndef :: GenParser Char st ()
matchUndef = P.reserved lexer "undef"
matchEach :: GenParser Char st ()
matchEach = P.reserved lexer "each"
matchTok :: Char -> GenParser Char st String
matchTok x = do
y <- char x
_ <- P.whiteSpace lexer
pure [y]
matchColon :: GenParser Char st String
matchColon = colon lexer
matchSemi :: GenParser Char st String
matchSemi = semi lexer
matchComma :: GenParser Char st String
matchComma = comma lexer
matchLE :: GenParser Char st String
matchLE = P.reservedOp lexer "<=" >> pure "<="
matchLT :: GenParser Char st String
matchLT = matchTok '<'
matchGE :: GenParser Char st String
matchGE = P.reservedOp lexer ">=" >> pure ">="
matchGT :: GenParser Char st String
matchGT = matchTok '>'
matchEQ :: GenParser Char st String
matchEQ = P.reservedOp lexer "==" >> pure "=="
matchNE :: GenParser Char st String
matchNE = P.reservedOp lexer "!=" >> pure "!="
matchAND :: GenParser Char st String
matchAND = P.reservedOp lexer "&&" >> pure "&&"
matchOR :: GenParser Char st String
matchOR = P.reservedOp lexer "||" >> pure "||"
matchCAT :: GenParser Char st String
matchCAT = P.reservedOp lexer "++" >> pure "++"
surroundedBy :: Char -> GenParser Char st a -> Char -> GenParser Char st a
surroundedBy leftTok middle rightTok = between (matchTok leftTok) (matchTok rightTok) middle
matchIdentifier :: GenParser Char st String
matchIdentifier = P.identifier lexer