From eb91d7c6b2010be6a43de0a978373654ba3deacc Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Tue, 1 Mar 2016 15:48:55 +0100 Subject: [PATCH] comments parsen is kapot Signed-off-by: Mart Lubbers --- src/example.spl | 2 +- src/lex.dcl | 2 +- src/lex.icl | 6 ++-- src/main.icl | 53 ++++++++++++++++++-------------- src/main.prj | 81 +++++++++++++++++++++++++------------------------ src/parse.icl | 4 +-- 6 files changed, 78 insertions(+), 70 deletions(-) diff --git a/src/example.spl b/src/example.spl index 2a1f3ab..b4efb27 100644 --- a/src/example.spl +++ b/src/example.spl @@ -93,4 +93,4 @@ squareOddNumbers(list) :: [Int] -> [Int] { } return list; } -//deze comment eindigt met EOF ipv newline \ No newline at end of file +//deze comment eindigt met EOF ipv newline diff --git a/src/lex.dcl b/src/lex.dcl index a713e5d..df25ea8 100644 --- a/src/lex.dcl +++ b/src/lex.dcl @@ -5,7 +5,7 @@ from Data.Either import :: Either :: Token :== (Int, Int, TokenValue) :: TokenValue //Value tokens - = IdentToken [Char] // Identifier + = IdentToken String // Identifier | NumberToken Int // Integer | CharToken Char // Character literal //Keyword tokens diff --git a/src/lex.icl b/src/lex.icl index bedb75f..4cf915c 100644 --- a/src/lex.icl +++ b/src/lex.icl @@ -16,7 +16,7 @@ lexer r = case runParser lexProgram r of lexProgram :: Parser Char [Token] lexProgram = some lexToken <* many (satisfy isSpace) <* eof - >>= \ts->pure (map (\t->(0, 0, t)) ts) + >>= \ts->pure $ (map (\t->(0, 0, t)) ts) ++ [(0, 0, EndOfFileToken)] lexToken :: Parser Char TokenValue lexToken = @@ -59,10 +59,10 @@ lexToken = //Number tokens (liftM (NumberToken o toInt o toString) $ some $ satisfy isDigit) <|> //Ident tokens - (liftM IdentToken $ some $ satisfy isIdentChar) <|> + (liftM (IdentToken o toString) $ some $ satisfy isIdentChar) <|> (satisfy isSpace >>| lexToken) where - lexUntilNL = top until ((item '\n' >>| return Void) <|> eof) + lexUntilNL = top until (eof <|> (item '\n' >>| pure Void)) lexUntilCommentClose = top until list (fromString "*/") isIdentChar c = isAlphanum c || c == '_' lexOp s tv = list (fromString s) >>| pure tv diff --git a/src/main.icl b/src/main.icl index f2b5c93..0dfab03 100644 --- a/src/main.icl +++ b/src/main.icl @@ -4,11 +4,15 @@ import StdFile import StdBool import StdMisc import StdFunc +import StdTuple import StdList import StdString import Data.Either import Data.Maybe +import Data.Func import System.CommandLine +import GenPrint +from Text import class Text(concat), instance Text String import parse import lex @@ -20,45 +24,48 @@ import lex fp :: Maybe String, help :: Bool} -:: *StartType :== (LexerOutput, ParserOutput, *World) +derive gPrint TokenValue -Start :: *World -> *StartType +Start :: *World -> *World Start w # (args, w) = parseArgs w | args.help # (out, w) = stdio w - # out = out <<< "\nUsage: " <<< args.program <<< " [opts] [FILENAME]\n" + # out = out <<< "Usage: " <<< args.program <<< " [opts] [FILENAME]\n" <<< "\t--help Show this help\n" - <<< "\t--lex Lex only, is mutually exclusive with --parse\n" - <<< "\t--parse Lex & Parse only\n\n" + <<< "\t--[no-]lex Lexer output(default: disabled)\n" + <<< "\t--[no-]parse Parser output(default: enabled)\n\n" <<< "\tFILENAME File to parse, when unspecified stdin is parsed\n" - # (_, w) = fclose out w - = (Left "", Left "", w) + = snd $ fclose out w # (stdin, w) = stdio w # (contents, stdin, w) = readFileOrStdin stdin args.fp w -| args.lex = case contents of - (Right cs) = (lexer cs, Left "Parsing Disabled", w) - (Left e) = (Left e, Left "Parsing disabled", w) = case contents of - (Left e) = (Left e, Left "", w) + (Left cs) = snd $ fclose (stdin <<< cs) w (Right cs) - # lexout = lexer cs - # parsout = parser lexout - # stdin = stdin <<< (either (const "") toString parsout) - # (_, w) = fclose stdin w - = (lexout, parser lexout, w) + # lexOut = lexer cs + # stdin = if (not args.lex) stdin (case lexOut of + (Left lex) = stdin <<< toString lex + (Right toks) = stdin <<< + concat (map (\(_, _, t)->printToString t +++ "\n") toks)) + # parseOut = parser lexOut + # stdin = if (not args.parse) stdin (case parser lexOut of + (Left parse) = stdin <<< toString parse + (Right ast) = stdin <<< toString ast) + = snd $ fclose stdin w parseArgs :: *World -> (Opts, *World) parseArgs w # ([p:args], w) = getCommandLine w -= (pa args {program=p, lex=False, parse=False, fp=Nothing, help=False}, w) += (pa args {program=p, lex=False, parse=True, fp=Nothing, help=False}, w) where - pa :: [String] -> (Opts -> Opts) - pa [] = id - pa ["--help":r] = \o.pa r {o & help=True} - pa ["--lex":r] = \o.pa r {o & lex=True, parse=False} - pa ["--parse":r] = \o.pa r {o & lex=False, parse=True} - pa [x:r] = \o.pa r {o & fp=Just x} + pa :: [String] Opts -> Opts + pa [] o = o + pa ["--help":r] o = pa r {o & help=True} + pa ["--lex":r] o = pa r {o & lex=True} + pa ["--no-lex":r] o = pa r {o & lex=False} + pa ["--parse":r] o = pa r {o & parse=True} + pa ["--no-parse":r] o = pa r {o & parse=False} + pa [x:r] o = pa r {o & fp=Just x} readFileOrStdin :: *File (Maybe String) *World -> *(Either String [Char], *File, *World) readFileOrStdin stdin Nothing w diff --git a/src/main.prj b/src/main.prj index d7bf8d0..933ed3d 100644 --- a/src/main.prj +++ b/src/main.prj @@ -24,13 +24,14 @@ Global Time: True Stack: True Output - Output: ShowConstructors + Output: NoConsole Font: Courier FontSize: 9 WriteStdErr: False Link LinkMethod: Static GenerateRelocations: False + GenerateSymbolTable: False GenerateLinkMap: False LinkResources: False ResourceSource: @@ -364,8 +365,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Control.Applicative - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: System.OS + Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Linux-64 Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -378,8 +379,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Control.Monad - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: System.OS + Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Linux-64 Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -392,8 +393,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Data.Either - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Control.Applicative + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -406,8 +407,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Data.Func - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Control.Monad + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -420,8 +421,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Data.Functor - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Data.Either + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -434,8 +435,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Data.List - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Data.Func + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -448,8 +449,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Data.Maybe - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Data.Functor + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -462,8 +463,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Data.Monoid - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Data.List + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -476,8 +477,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Data.Void - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Data.Maybe + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -490,8 +491,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: System.CommandLine - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Data.Monoid + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -504,8 +505,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: System.IO - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: Data.Void + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -518,8 +519,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: System._Pointer - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: System.CommandLine + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -532,8 +533,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: Text - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Independent + Name: System.IO + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -546,8 +547,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: System.OS - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Linux-64 + Name: System._Pointer + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -560,8 +561,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: System.OS - Dir: {Application}/lib/iTasks-SDK/Dependencies/Platform/OS-Linux-64 + Name: Text + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -574,8 +575,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: _SystemDynamic - Dir: {Application}/lib/iTasks-SDK/Patches/Dynamics + Name: StdMaybe + Dir: {Application}/lib/iTasks-SDK/Dependencies/clean-platform/src/libraries/OS-Independent/Deprecated/StdLib Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -588,8 +589,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: GenEq - Dir: {Application}/lib/iTasks-SDK/Patches/Generics + Name: _SystemDynamic + Dir: {Application}/lib/iTasks-SDK/Patches/Dynamics Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -602,7 +603,7 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: GenPrint + Name: GenEq Dir: {Application}/lib/iTasks-SDK/Patches/Generics Compiler NeverMemoryProfile: False @@ -616,8 +617,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: StdGeneric - Dir: {Application}/lib/iTasks-SDK/Patches/StdEnv + Name: GenPrint + Dir: {Application}/lib/iTasks-SDK/Patches/Generics Compiler NeverMemoryProfile: False NeverTimeProfile: False @@ -630,8 +631,8 @@ OtherModules ReuseUniqueNodes: True Fusion: False Module - Name: StdMaybe - Dir: {Application}/lib/iTasks-SDK/Server/lib + Name: StdGeneric + Dir: {Application}/lib/iTasks-SDK/Patches/StdEnv Compiler NeverMemoryProfile: False NeverTimeProfile: False diff --git a/src/parse.icl b/src/parse.icl index 042ec47..ec89565 100644 --- a/src/parse.icl +++ b/src/parse.icl @@ -25,7 +25,7 @@ parser (Right r) = case runParser parseProgram r of parseProgram :: Parser Token AST parseProgram = liftM2 AST (many parseVarDecl) (some parseFunDecl) -// <* satTok EndOfFileToken + <* satTok EndOfFileToken parseFunDecl :: Parser Token FunDecl parseFunDecl = liftM5 FunDecl @@ -191,7 +191,7 @@ parseSepList sep p = (liftM pure p) <|> pure empty parseIdent :: Parser Token String -parseIdent = trans2 (IdentToken []) (\(IdentToken e).toString e) +parseIdent = trans2 (IdentToken "") (\(IdentToken e).toString e) instance toString AST where toString (AST v f) = concat ( -- 2.20.1