Skip to content

Instantly share code, notes, and snippets.

@fables-tales
Created April 24, 2012 19:07
Show Gist options
  • Save fables-tales/2482767 to your computer and use it in GitHub Desktop.
Save fables-tales/2482767 to your computer and use it in GitHub Desktop.
module Parser where
-- import Text.ParserCombinators.Parsec
type Var = String
type Value = String
type Assign = (Var, Value)
type Object = [Assign]
type Array = [Value]
data Token = Open_object | Close_object | Open_array | Close_array | List_delim |
Assign_delim | Quote | String_literal String deriving (Show, Eq, Read)
tokens :: [(String, Token)]
tokens = [
('{', Open_object),
('}', Close_object),
('[', Open_array),
(']', Close_array),
(',', List_delim),
(':', Assign_delim),
('"', Quote)
]
token_list = map fst tokens
--open_object = "{"
--close_object = "}"
--open_array = "["
--close_array = "]"
--list_delim = ","
--assign_delim = ":"
--quote = "\""
object_sample = "{\"var\": \"val\"}"
array_sample = "[\"x1\", \"x2\"]"
tokenize :: String -> [Token]
tokenize "" = []
tokenize (a : rest) = let token = lookup a tokens in
case token of
Nothing -> takeWhile (\x -> x \= '"') rest
Just t -> t : tokenize rest
main = do
print "hello json"
print (tokenize array_sample)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment