Commit 2e3f671c authored by Kenton Varda's avatar Kenton Varda

class -> struct

parent 22a75445
...@@ -105,7 +105,7 @@ statusToMaybe (Failed _) = Nothing ...@@ -105,7 +105,7 @@ statusToMaybe (Failed _) = Nothing
-- | Look up a direct member of a descriptor by name. -- | Look up a direct member of a descriptor by name.
descMember name (DescFile d) = lookupMember name (fileMemberMap d) descMember name (DescFile d) = lookupMember name (fileMemberMap d)
descMember name (DescEnum d) = lookupMember name (enumMemberMap d) descMember name (DescEnum d) = lookupMember name (enumMemberMap d)
descMember name (DescClass d) = lookupMember name (classMemberMap d) descMember name (DescStruct d) = lookupMember name (structMemberMap d)
descMember name (DescInterface d) = lookupMember name (interfaceMemberMap d) descMember name (DescInterface d) = lookupMember name (interfaceMemberMap d)
descMember name (DescAlias d) = descMember name (aliasTarget d) descMember name (DescAlias d) = descMember name (aliasTarget d)
descMember _ _ = Nothing descMember _ _ = Nothing
...@@ -191,7 +191,7 @@ compileValue pos (BuiltinType BuiltinText) _ = makeExpectError pos "string" ...@@ -191,7 +191,7 @@ compileValue pos (BuiltinType BuiltinText) _ = makeExpectError pos "string"
compileValue pos (BuiltinType BuiltinBytes) _ = makeExpectError pos "string" compileValue pos (BuiltinType BuiltinBytes) _ = makeExpectError pos "string"
compileValue pos (EnumType _) _ = makeError pos "Unimplemented: enum default values" compileValue pos (EnumType _) _ = makeError pos "Unimplemented: enum default values"
compileValue pos (ClassType _) _ = makeError pos "Unimplemented: class default values" compileValue pos (StructType _) _ = makeError pos "Unimplemented: struct default values"
compileValue pos (InterfaceType _) _ = makeError pos "Interfaces can't have default values." compileValue pos (InterfaceType _) _ = makeError pos "Interfaces can't have default values."
compileValue pos (ListType _) _ = makeError pos "Unimplemented: array default values" compileValue pos (ListType _) _ = makeError pos "Unimplemented: array default values"
...@@ -200,11 +200,11 @@ makeFileMemberMap desc = Map.fromList allMembers where ...@@ -200,11 +200,11 @@ makeFileMemberMap desc = Map.fromList allMembers where
allMembers = [ (aliasName m, DescAlias m) | m <- fileAliases desc ] allMembers = [ (aliasName m, DescAlias m) | m <- fileAliases desc ]
++ [ (constantName m, DescConstant m) | m <- fileConstants desc ] ++ [ (constantName m, DescConstant m) | m <- fileConstants desc ]
++ [ (enumName m, DescEnum m) | m <- fileEnums desc ] ++ [ (enumName m, DescEnum m) | m <- fileEnums desc ]
++ [ (className m, DescClass m) | m <- fileClasses desc ] ++ [ (structName m, DescStruct m) | m <- fileStructs desc ]
++ [ (interfaceName m, DescInterface m) | m <- fileInterfaces desc ] ++ [ (interfaceName m, DescInterface m) | m <- fileInterfaces desc ]
descAsType _ (DescEnum desc) = succeed (EnumType desc) descAsType _ (DescEnum desc) = succeed (EnumType desc)
descAsType _ (DescClass desc) = succeed (ClassType desc) descAsType _ (DescStruct desc) = succeed (StructType desc)
descAsType _ (DescInterface desc) = succeed (InterfaceType desc) descAsType _ (DescInterface desc) = succeed (InterfaceType desc)
descAsType _ (DescBuiltinType desc) = succeed (BuiltinType desc) descAsType _ (DescBuiltinType desc) = succeed (BuiltinType desc)
descAsType name (DescAlias desc) = descAsType name (aliasTarget desc) descAsType name (DescAlias desc) = descAsType name (aliasTarget desc)
...@@ -286,21 +286,21 @@ compileDecl scope (EnumValueDecl (Located _ name) (Located _ number) decls) = ...@@ -286,21 +286,21 @@ compileDecl scope (EnumValueDecl (Located _ name) (Located _ number) decls) =
, enumValueOptions = options , enumValueOptions = options
}))) })))
compileDecl scope (ClassDecl (Located _ name) decls) = compileDecl scope (StructDecl (Located _ name) decls) =
CompiledMember name (feedback (\desc -> do CompiledMember name (feedback (\desc -> do
(members, memberMap, options) <- compileChildDecls desc decls (members, memberMap, options) <- compileChildDecls desc decls
return (DescClass ClassDesc return (DescStruct StructDesc
{ className = name { structName = name
, classParent = scope , structParent = scope
, classFields = [d | DescField d <- members] , structFields = [d | DescField d <- members]
, classNestedAliases = [d | DescAlias d <- members] , structNestedAliases = [d | DescAlias d <- members]
, classNestedConstants = [d | DescConstant d <- members] , structNestedConstants = [d | DescConstant d <- members]
, classNestedEnums = [d | DescEnum d <- members] , structNestedEnums = [d | DescEnum d <- members]
, classNestedClasses = [d | DescClass d <- members] , structNestedStructs = [d | DescStruct d <- members]
, classNestedInterfaces = [d | DescInterface d <- members] , structNestedInterfaces = [d | DescInterface d <- members]
, classOptions = options , structOptions = options
, classMembers = members , structMembers = members
, classMemberMap = memberMap , structMemberMap = memberMap
}))) })))
compileDecl scope (FieldDecl (Located _ name) (Located _ number) typeExp defaultValue decls) = compileDecl scope (FieldDecl (Located _ name) (Located _ number) typeExp defaultValue decls) =
...@@ -329,7 +329,7 @@ compileDecl scope (InterfaceDecl (Located _ name) decls) = ...@@ -329,7 +329,7 @@ compileDecl scope (InterfaceDecl (Located _ name) decls) =
, interfaceNestedAliases = [d | DescAlias d <- members] , interfaceNestedAliases = [d | DescAlias d <- members]
, interfaceNestedConstants = [d | DescConstant d <- members] , interfaceNestedConstants = [d | DescConstant d <- members]
, interfaceNestedEnums = [d | DescEnum d <- members] , interfaceNestedEnums = [d | DescEnum d <- members]
, interfaceNestedClasses = [d | DescClass d <- members] , interfaceNestedStructs = [d | DescStruct d <- members]
, interfaceNestedInterfaces = [d | DescInterface d <- members] , interfaceNestedInterfaces = [d | DescInterface d <- members]
, interfaceOptions = options , interfaceOptions = options
, interfaceMembers = members , interfaceMembers = members
...@@ -378,7 +378,7 @@ compileFile name decls = ...@@ -378,7 +378,7 @@ compileFile name decls =
, fileAliases = [d | DescAlias d <- members] , fileAliases = [d | DescAlias d <- members]
, fileConstants = [d | DescConstant d <- members] , fileConstants = [d | DescConstant d <- members]
, fileEnums = [d | DescEnum d <- members] , fileEnums = [d | DescEnum d <- members]
, fileClasses = [d | DescClass d <- members] , fileStructs = [d | DescStruct d <- members]
, fileInterfaces = [d | DescInterface d <- members] , fileInterfaces = [d | DescInterface d <- members]
, fileOptions = options , fileOptions = options
, fileMembers = members , fileMembers = members
......
...@@ -47,7 +47,7 @@ data Declaration = AliasDecl (Located String) DeclName ...@@ -47,7 +47,7 @@ data Declaration = AliasDecl (Located String) DeclName
| ConstantDecl (Located String) TypeExpression (Located FieldValue) | ConstantDecl (Located String) TypeExpression (Located FieldValue)
| EnumDecl (Located String) [Declaration] | EnumDecl (Located String) [Declaration]
| EnumValueDecl (Located String) (Located Integer) [Declaration] | EnumValueDecl (Located String) (Located Integer) [Declaration]
| ClassDecl (Located String) [Declaration] | StructDecl (Located String) [Declaration]
| FieldDecl (Located String) (Located Integer) | FieldDecl (Located String) (Located Integer)
TypeExpression (Maybe (Located FieldValue)) [Declaration] TypeExpression (Maybe (Located FieldValue)) [Declaration]
| InterfaceDecl (Located String) [Declaration] | InterfaceDecl (Located String) [Declaration]
......
...@@ -35,7 +35,7 @@ keywords = ...@@ -35,7 +35,7 @@ keywords =
, (UsingKeyword, "using") , (UsingKeyword, "using")
, (ConstKeyword, "const") , (ConstKeyword, "const")
, (EnumKeyword, "enum") , (EnumKeyword, "enum")
, (ClassKeyword, "class") , (StructKeyword, "struct")
, (InterfaceKeyword, "interface") , (InterfaceKeyword, "interface")
, (OptionKeyword, "option") , (OptionKeyword, "option")
] ]
......
...@@ -62,7 +62,7 @@ importKeyword = tokenParser (matchSimpleToken ImportKeyword) ...@@ -62,7 +62,7 @@ importKeyword = tokenParser (matchSimpleToken ImportKeyword)
usingKeyword = tokenParser (matchSimpleToken UsingKeyword) usingKeyword = tokenParser (matchSimpleToken UsingKeyword)
constKeyword = tokenParser (matchSimpleToken ConstKeyword) constKeyword = tokenParser (matchSimpleToken ConstKeyword)
enumKeyword = tokenParser (matchSimpleToken EnumKeyword) enumKeyword = tokenParser (matchSimpleToken EnumKeyword)
classKeyword = tokenParser (matchSimpleToken ClassKeyword) structKeyword = tokenParser (matchSimpleToken StructKeyword)
interfaceKeyword = tokenParser (matchSimpleToken InterfaceKeyword) interfaceKeyword = tokenParser (matchSimpleToken InterfaceKeyword)
optionKeyword = tokenParser (matchSimpleToken OptionKeyword) optionKeyword = tokenParser (matchSimpleToken OptionKeyword)
...@@ -111,7 +111,7 @@ constantDecl = do ...@@ -111,7 +111,7 @@ constantDecl = do
return (ConstantDecl name typeName value) return (ConstantDecl name typeName value)
typeDecl statements = enumDecl statements typeDecl statements = enumDecl statements
<|> classDecl statements <|> structDecl statements
<|> interfaceDecl statements <|> interfaceDecl statements
enumDecl statements = do enumDecl statements = do
...@@ -135,15 +135,15 @@ enumValueLine :: Maybe [Located Statement] -> TokenParser Declaration ...@@ -135,15 +135,15 @@ enumValueLine :: Maybe [Located Statement] -> TokenParser Declaration
enumValueLine Nothing = optionDecl enumValueLine Nothing = optionDecl
enumValueLine (Just _) = fail "Blocks not allowed here." enumValueLine (Just _) = fail "Blocks not allowed here."
classDecl statements = do structDecl statements = do
classKeyword structKeyword
name <- located identifier name <- located identifier
children <- parseBlock classLine statements children <- parseBlock structLine statements
return (ClassDecl name children) return (StructDecl name children)
classLine :: Maybe [Located Statement] -> TokenParser Declaration structLine :: Maybe [Located Statement] -> TokenParser Declaration
classLine Nothing = optionDecl <|> constantDecl <|> fieldDecl [] structLine Nothing = optionDecl <|> constantDecl <|> fieldDecl []
classLine (Just statements) = typeDecl statements <|> fieldDecl statements structLine (Just statements) = typeDecl statements <|> fieldDecl statements
fieldDecl statements = do fieldDecl statements = do
name <- located identifier name <- located identifier
......
...@@ -38,7 +38,7 @@ data Desc = DescFile FileDesc ...@@ -38,7 +38,7 @@ data Desc = DescFile FileDesc
| DescConstant ConstantDesc | DescConstant ConstantDesc
| DescEnum EnumDesc | DescEnum EnumDesc
| DescEnumValue EnumValueDesc | DescEnumValue EnumValueDesc
| DescClass ClassDesc | DescStruct StructDesc
| DescField FieldDesc | DescField FieldDesc
| DescInterface InterfaceDesc | DescInterface InterfaceDesc
| DescMethod MethodDesc | DescMethod MethodDesc
...@@ -51,7 +51,7 @@ descName (DescAlias d) = aliasName d ...@@ -51,7 +51,7 @@ descName (DescAlias d) = aliasName d
descName (DescConstant d) = constantName d descName (DescConstant d) = constantName d
descName (DescEnum d) = enumName d descName (DescEnum d) = enumName d
descName (DescEnumValue d) = enumValueName d descName (DescEnumValue d) = enumValueName d
descName (DescClass d) = className d descName (DescStruct d) = structName d
descName (DescField d) = fieldName d descName (DescField d) = fieldName d
descName (DescInterface d) = interfaceName d descName (DescInterface d) = interfaceName d
descName (DescMethod d) = methodName d descName (DescMethod d) = methodName d
...@@ -64,7 +64,7 @@ descParent (DescAlias d) = aliasParent d ...@@ -64,7 +64,7 @@ descParent (DescAlias d) = aliasParent d
descParent (DescConstant d) = constantParent d descParent (DescConstant d) = constantParent d
descParent (DescEnum d) = enumParent d descParent (DescEnum d) = enumParent d
descParent (DescEnumValue d) = enumValueParent d descParent (DescEnumValue d) = enumValueParent d
descParent (DescClass d) = classParent d descParent (DescStruct d) = structParent d
descParent (DescField d) = fieldParent d descParent (DescField d) = fieldParent d
descParent (DescInterface d) = interfaceParent d descParent (DescInterface d) = interfaceParent d
descParent (DescMethod d) = methodParent d descParent (DescMethod d) = methodParent d
...@@ -123,7 +123,7 @@ valueString (BytesDesc s) = show (map (chr . fromIntegral) s) ...@@ -123,7 +123,7 @@ valueString (BytesDesc s) = show (map (chr . fromIntegral) s)
data TypeDesc = BuiltinType BuiltinType data TypeDesc = BuiltinType BuiltinType
| EnumType EnumDesc | EnumType EnumDesc
| ClassType ClassDesc | StructType StructDesc
| InterfaceType InterfaceDesc | InterfaceType InterfaceDesc
| ListType TypeDesc | ListType TypeDesc
...@@ -131,7 +131,7 @@ data TypeDesc = BuiltinType BuiltinType ...@@ -131,7 +131,7 @@ data TypeDesc = BuiltinType BuiltinType
typeName :: Desc -> TypeDesc -> String typeName :: Desc -> TypeDesc -> String
typeName _ (BuiltinType t) = builtinTypeName t -- TODO: Check for shadowing. typeName _ (BuiltinType t) = builtinTypeName t -- TODO: Check for shadowing.
typeName scope (EnumType desc) = descQualifiedName scope (DescEnum desc) typeName scope (EnumType desc) = descQualifiedName scope (DescEnum desc)
typeName scope (ClassType desc) = descQualifiedName scope (DescClass desc) typeName scope (StructType desc) = descQualifiedName scope (DescStruct desc)
typeName scope (InterfaceType desc) = descQualifiedName scope (DescInterface desc) typeName scope (InterfaceType desc) = descQualifiedName scope (DescInterface desc)
typeName scope (ListType t) = "List(" ++ typeName scope t ++ ")" typeName scope (ListType t) = "List(" ++ typeName scope t ++ ")"
...@@ -159,7 +159,7 @@ data FileDesc = FileDesc ...@@ -159,7 +159,7 @@ data FileDesc = FileDesc
, fileAliases :: [AliasDesc] , fileAliases :: [AliasDesc]
, fileConstants :: [ConstantDesc] , fileConstants :: [ConstantDesc]
, fileEnums :: [EnumDesc] , fileEnums :: [EnumDesc]
, fileClasses :: [ClassDesc] , fileStructs :: [StructDesc]
, fileInterfaces :: [InterfaceDesc] , fileInterfaces :: [InterfaceDesc]
, fileOptions :: OptionMap , fileOptions :: OptionMap
, fileMembers :: [Desc] , fileMembers :: [Desc]
...@@ -196,18 +196,18 @@ data EnumValueDesc = EnumValueDesc ...@@ -196,18 +196,18 @@ data EnumValueDesc = EnumValueDesc
, enumValueOptions :: OptionMap , enumValueOptions :: OptionMap
} }
data ClassDesc = ClassDesc data StructDesc = StructDesc
{ className :: String { structName :: String
, classParent :: Desc , structParent :: Desc
, classFields :: [FieldDesc] , structFields :: [FieldDesc]
, classNestedAliases :: [AliasDesc] , structNestedAliases :: [AliasDesc]
, classNestedConstants :: [ConstantDesc] , structNestedConstants :: [ConstantDesc]
, classNestedEnums :: [EnumDesc] , structNestedEnums :: [EnumDesc]
, classNestedClasses :: [ClassDesc] , structNestedStructs :: [StructDesc]
, classNestedInterfaces :: [InterfaceDesc] , structNestedInterfaces :: [InterfaceDesc]
, classOptions :: OptionMap , structOptions :: OptionMap
, classMembers :: [Desc] , structMembers :: [Desc]
, classMemberMap :: MemberMap , structMemberMap :: MemberMap
} }
data FieldDesc = FieldDesc data FieldDesc = FieldDesc
...@@ -226,7 +226,7 @@ data InterfaceDesc = InterfaceDesc ...@@ -226,7 +226,7 @@ data InterfaceDesc = InterfaceDesc
, interfaceNestedAliases :: [AliasDesc] , interfaceNestedAliases :: [AliasDesc]
, interfaceNestedConstants :: [ConstantDesc] , interfaceNestedConstants :: [ConstantDesc]
, interfaceNestedEnums :: [EnumDesc] , interfaceNestedEnums :: [EnumDesc]
, interfaceNestedClasses :: [ClassDesc] , interfaceNestedStructs :: [StructDesc]
, interfaceNestedInterfaces :: [InterfaceDesc] , interfaceNestedInterfaces :: [InterfaceDesc]
, interfaceOptions :: OptionMap , interfaceOptions :: OptionMap
, interfaceMembers :: [Desc] , interfaceMembers :: [Desc]
...@@ -273,9 +273,9 @@ descToCode indent (DescEnum desc) = printf "%senum %s {\n%s%s}\n" indent ...@@ -273,9 +273,9 @@ descToCode indent (DescEnum desc) = printf "%senum %s {\n%s%s}\n" indent
indent indent
descToCode indent (DescEnumValue desc) = printf "%s%s = %d;\n" indent descToCode indent (DescEnumValue desc) = printf "%s%s = %d;\n" indent
(enumValueName desc) (enumValueNumber desc) (enumValueName desc) (enumValueNumber desc)
descToCode indent (DescClass desc) = printf "%sclass %s {\n%s%s}\n" indent descToCode indent (DescStruct desc) = printf "%sstruct %s {\n%s%s}\n" indent
(className desc) (structName desc)
(concatMap (descToCode (" " ++ indent)) (classMembers desc)) (concatMap (descToCode (" " ++ indent)) (structMembers desc))
indent indent
descToCode indent (DescField desc) = printf "%s%s@%d: %s%s;\n" indent descToCode indent (DescField desc) = printf "%s%s@%d: %s%s;\n" indent
(fieldName desc) (fieldNumber desc) (fieldName desc) (fieldNumber desc)
...@@ -303,7 +303,7 @@ instance Show AliasDesc where { show desc = descToCode "" (DescAlias desc) } ...@@ -303,7 +303,7 @@ instance Show AliasDesc where { show desc = descToCode "" (DescAlias desc) }
instance Show ConstantDesc where { show desc = descToCode "" (DescConstant desc) } instance Show ConstantDesc where { show desc = descToCode "" (DescConstant desc) }
instance Show EnumDesc where { show desc = descToCode "" (DescEnum desc) } instance Show EnumDesc where { show desc = descToCode "" (DescEnum desc) }
instance Show EnumValueDesc where { show desc = descToCode "" (DescEnumValue desc) } instance Show EnumValueDesc where { show desc = descToCode "" (DescEnumValue desc) }
instance Show ClassDesc where { show desc = descToCode "" (DescClass desc) } instance Show StructDesc where { show desc = descToCode "" (DescStruct desc) }
instance Show FieldDesc where { show desc = descToCode "" (DescField desc) } instance Show FieldDesc where { show desc = descToCode "" (DescField desc) }
instance Show InterfaceDesc where { show desc = descToCode "" (DescInterface desc) } instance Show InterfaceDesc where { show desc = descToCode "" (DescInterface desc) }
instance Show MethodDesc where { show desc = descToCode "" (DescMethod desc) } instance Show MethodDesc where { show desc = descToCode "" (DescMethod desc) }
...@@ -45,7 +45,7 @@ data Token = Identifier String ...@@ -45,7 +45,7 @@ data Token = Identifier String
| UsingKeyword | UsingKeyword
| ConstKeyword | ConstKeyword
| EnumKeyword | EnumKeyword
| ClassKeyword | StructKeyword
| InterfaceKeyword | InterfaceKeyword
| OptionKeyword | OptionKeyword
deriving (Show, Eq) deriving (Show, Eq)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment