From 4b8ac77a88528dd1eeac43fd7a6c2e6ac711d5a9 Mon Sep 17 00:00:00 2001 From: pimjager Date: Thu, 11 Feb 2016 21:44:08 +0100 Subject: [PATCH] ported Yard to clean --- lex.dcl | 4 ++-- lex.icl | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- lex.prj | 69 +++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 129 insertions(+), 16 deletions(-) diff --git a/lex.dcl b/lex.dcl index 3abe22d..f256af5 100644 --- a/lex.dcl +++ b/lex.dcl @@ -44,6 +44,6 @@ import Data.Either | AmpersandsToken // && | PipesToken // || -:: LexerOutput :== Either String [Token] +:: LexerOutput a :== Either String a -lexer :: [Char] -> LexerOutput +lexer :: [Char] -> LexerOutput [Token] diff --git a/lex.icl b/lex.icl index cc4bc2f..1475a0e 100644 --- a/lex.icl +++ b/lex.icl @@ -6,12 +6,18 @@ import StdString import System.CommandLine import StdFile import StdMisc +from StdFunc import id, const +import Data.Maybe +import Control.Applicative +import Control.Monad +import Control.Monad.State +from Data.Func import $ // Misschien moeten we hier continuation style van maken instance toString lexerOutput where toString l = "dit is een lexer output, danwel error\n" -lexer :: [Char] -> LexerOutput +lexer :: [Char] -> LexerOutput [Token] lexer _ = Left "Not Implemented" Start :: *World -> *World @@ -30,3 +36,67 @@ Start w | not b = ([], f) # (cs, f) = readEntireFile f = ([c:cs], f) + + + +// Clean adaption of Yard, a parsec like parser combinator +:: Parser a = Parser ([Char] -> (LexerOutput a, [Char])) + +runParser :: (Parser a) [Char] -> (LexerOutput a, [Char]) +runParser (Parser p) s = p s + +instance Functor Parser where + fmap f s = liftM f s + +instance Applicative Parser where + pure a = Parser $ \s -> (Right a, s) + (<*>) sf s = ap sf s + +instance Monad Parser where + bind p f = Parser $ \s -> let (out, rest) = runParser p s in case out of + Left e = (Left e, rest) + Right t = runParser (f t) rest + +//gives us some, many and optional +instance Alternative Parser where + empty = zero + (<|>) p1 p2 = parserAlternative p1 p2 +parserAlternative p1 p2 = Parser $ \s -> let (out, rest) = runParser p1 s in case out of + Left e = runParser p2 s + Right t = (Right t, rest) + +//parser that fails with error +fail :: String -> Parser a +fail e = Parser $ \s -> (Left e, s) + +//parser that always fails +zero :: Parser a +zero = fail "Zero parser" + +//matches exactly one Char +item :: Parser Char +item = Parser $ \s -> case s of + [] = (Left "Unexpected empty input", s) + [x:xs] = (Right x, xs) + +//matches any char which satisfies f +satisfy :: (Char -> Bool) -> Parser Char +satisfy f = item >>= (\r -> if (f r) (return r) zero) + +//tries a parser, if it fails returns a default value +optionalDef :: a (Parser a) -> Parser a +optionalDef def p = parserAlternative p (return def) + +//matched given char +char :: Char -> Parser Char +char c = satisfy (\i -> c==i) //I hate that we can't do: satisfy (==c) + +alpha :: Parser Char +alpha = satisfy isAlpha + +digit :: Parser Char +digit = satisfy isDigit + +//matches a given String +string :: [Char] -> Parser [Char] +string s = mapM_ char s >>| return s \ No newline at end of file diff --git a/lex.prj b/lex.prj index 4fc8d41..15b5f46 100644 --- a/lex.prj +++ b/lex.prj @@ -31,6 +31,7 @@ Global Link LinkMethod: Static GenerateRelocations: False + GenerateSymbolTable: False GenerateLinkMap: False LinkResources: False ResourceSource: @@ -323,7 +324,7 @@ OtherModules Fusion: False Module Name: Control.Applicative - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -337,7 +338,35 @@ OtherModules Fusion: False Module Name: Control.Monad - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False + Module + Name: Control.Monad.State + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False + Module + Name: Control.Monad.Trans + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -351,7 +380,7 @@ OtherModules Fusion: False Module Name: Data.Either - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -365,7 +394,7 @@ OtherModules Fusion: False Module Name: Data.Func - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -379,7 +408,21 @@ OtherModules Fusion: False Module Name: Data.Functor - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent + Compiler + NeverMemoryProfile: False + NeverTimeProfile: False + StrictnessAnalysis: True + ListTypes: StrictExportTypes + ListAttributes: True + Warnings: True + Verbose: True + ReadableABC: False + ReuseUniqueNodes: True + Fusion: False + Module + Name: Data.Functor.Identity + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -393,7 +436,7 @@ OtherModules Fusion: False Module Name: Data.List - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -407,7 +450,7 @@ OtherModules Fusion: False Module Name: Data.Maybe - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -421,7 +464,7 @@ OtherModules Fusion: False Module Name: Data.Monoid - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -435,7 +478,7 @@ OtherModules Fusion: False Module Name: Data.Void - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -449,7 +492,7 @@ OtherModules Fusion: False Module Name: System.CommandLine - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -463,7 +506,7 @@ OtherModules Fusion: False Module Name: System.IO - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -477,7 +520,7 @@ OtherModules Fusion: False Module Name: System._Pointer - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -491,7 +534,7 @@ OtherModules Fusion: False Module Name: System.OS - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Linux-64 + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Mac Compiler NeverMemoryProfile: False NeverTimeProfile: False -- 2.20.1