Commits

Kota UENISHI committed 35d5246

csv parser with parsec

  • Participants
  • Parent commits 5fef803

Comments (0)

Files changed (3)

haskell/OMakefile

-public.GHCFLAGS =
-public.GHC_ASFLAGS =
-public.GHC_LDFLAGS =
-public.GHC_INCLUDES =
-global.GHC = ghc
+public.HCFLAGS =
+public.HC_ASFLAGS =
+public.HC_LDFLAGS =
+public.HC_INCLUDES =
+global.HC_OPTS =
+global.HC = ghc
 
-public.GHCProgram(name, files) =
+%.o: %.hs
+	$(HC) -c $< $(HC_OPTS)
+
+public.HCProgram(name, files) =
    #
    # Generic program
    #
    if $(FLAGS)
       eprintln($""!!! WARNING: the LIBS variable should not include link flags "$(FLAGS)";"")
       eprintln($""!!!          those should go into LDFLAGS"")
-      GHC_LDFLAGS += $(FLAGS)
+      HC_LDFLAGS += $(FLAGS)
       LIBS = $(filter-out -%, $(LIBS))
       export
 
    private.LFILES = $(addsuffix $(EXT_LIB), $(LIBS))
 
    $(NAME): $(OFILES) $(LFILES)
-        $(GHC) $(GHCFLAGS) $(LDOUT)$@ $,(OFILES) $(LFILES) $(GHC_LDFLAGS)
+        $(HC) $(HCFLAGS) $(LDOUT)$@ $(OFILES) $(LFILES) $(HC_LDFLAGS) $(HC_OPTS)
 
    return $(NAME)
 
-public.StaticGHCLibrary(name, files) =
+public.StaticHCLibrary(name, files) =
     private.OFILES = $(addsuffix $(EXT_OBJ), $(files))
     private.NORMALLIB = $(file $(name)$(EXT_LIB))
 
     return $(NORMALLIB)
 
 ########################################################################
-# GHC configuration.
+# HC configuration.
 # Delete this section if you are not building C files.
 #
 
 #
 
 LDFLAGS += -read_only_relocs supress # if you use MacOS ?
-GHCFLAGS +=
-GHC_ASFLAGS +=
-GHC_LDFLAGS +=
-GHC_INCLUDES +=
+HCFLAGS +=
+HC_ASFLAGS +=
+HC_LDFLAGS +=
+HC_INCLUDES +=
+HC_OPTS = -threaded -cpp -package parsec
 
 ################################################
 # Uncomment the following section if you want
 # to build a C program in the current directory.
 #
 
-GHCFILES[] =
-    hello
+HCFILES[] =
+    mylang
 #    file1
 #    main
 #
-MAIN = main
+MAIN = mylang
 
-.DEFAULT: $(GHCProgram $(MAIN), $(GHCFILES))
+.DEFAULT: $(HCProgram $(MAIN), $(HCFILES))
+	./$(MAIN)
+
+.DEFAULT: $(HCProgram main, hello)
+.DEFAULT: $(HCProgram csvp, csvp)
 
 ################################################
 # Uncomment the following section if you want to build a C library
 #
 # LIB = libxxx
 #
-# .DEFAULT: $(StaticGHCLibrary $(LIB), $(LIBFILES))
+# .DEFAULT: $(StaticHCLibrary $(LIB), $(LIBFILES))
+import Text.ParserCombinators.Parsec
+
+main = putStrLn $ show $ parse_csv "'hoge',\"aaa asfd \"\nha ge, ge\n"
+
+data Expr a = Expr a
+            | Term a 
+            | EOF
+
+csvFile = endBy line eol
+
+line = sepBy cell (char ',')
+cell = quotedCell <|> quotedCell2 <|> many (noneOf ",\n\r")
+
+quotedCell = do
+  char '"'
+  content <- many quotedChar
+  char '"' <?> "quote at end of cell"
+  return content
+  
+quotedCell2 = do
+  char '\''
+  content <- many1 quotedChar
+  char '\'' <?> "quote at end of cell"
+  return content
+
+quotedChar = do  
+  noneOf "\"\'" <|> try (string "\"\"" >> return '"')
+
+eol = try (string "\n\r")
+      <|> try (string "\r\n")
+      <|> string "\n" 
+      <|> string "\r" 
+      <?> "couldn't find eol"
+
+parse_csv input = parse csvFile "(unknown)" input

haskell/mylang.hs

+import Text.ParserCombinators.Parsec
+
+main = putStrLn $ show $ parse_csv "'hoge',\"aaa asfd \"\nha ge, ge\n"
+
+data Expr a = Expr a
+            | Term a 
+            | EOF
+
+csvFile = endBy line eol
+
+line = sepBy cell (char ',')
+cell = quotedCell <|> quotedCell2 <|> many (noneOf ",\n\r")
+
+quotedCell = do
+  char '"'
+  content <- many quotedChar
+  char '"' <?> "quote at end of cell"
+  return content
+  
+quotedCell2 = do
+  char '\''
+  content <- many1 quotedChar
+  char '\'' <?> "quote at end of cell"
+  return content
+
+quotedChar = do  
+  noneOf "\"\'" <|> try (string "\"\"" >> return '"')
+
+eol = try (string "\n\r")
+      <|> try (string "\r\n")
+      <|> string "\n" 
+      <|> string "\r" 
+      <?> "couldn't find eol"
+
+parse_csv input = parse csvFile "(unknown)" input