Commit 4d121574 authored by Kenton Varda's avatar Kenton Varda

Implement annotations.

parent 7a7e0b64
This diff is collapsed.
...@@ -45,6 +45,10 @@ typeImports :: TypeExpression -> [Located String] ...@@ -45,6 +45,10 @@ typeImports :: TypeExpression -> [Located String]
typeImports (TypeExpression name params) = typeImports (TypeExpression name params) =
maybeToList (declNameImport name) ++ concatMap typeImports params maybeToList (declNameImport name) ++ concatMap typeImports params
data Annotation = Annotation DeclName (Located FieldValue) deriving(Show)
annotationImports (Annotation name _) = maybeToList $ declNameImport name
data FieldValue = VoidFieldValue data FieldValue = VoidFieldValue
| BoolFieldValue Bool | BoolFieldValue Bool
| IntegerFieldValue Integer | IntegerFieldValue Integer
...@@ -56,43 +60,75 @@ data FieldValue = VoidFieldValue ...@@ -56,43 +60,75 @@ data FieldValue = VoidFieldValue
| UnionFieldValue String FieldValue | UnionFieldValue String FieldValue
deriving (Show) deriving (Show)
data ParamDecl = ParamDecl String TypeExpression [Annotation] (Maybe (Located FieldValue))
deriving (Show)
paramImports (ParamDecl _ t ann _) = typeImports t ++ concatMap annotationImports ann
data AnnotationTarget = FileAnnotation
| ConstantAnnotation
| EnumAnnotation
| EnumValueAnnotation
| StructAnnotation
| FieldAnnotation
| UnionAnnotation
| InterfaceAnnotation
| MethodAnnotation
| ParamAnnotation
| AnnotationAnnotation
deriving(Eq, Ord, Bounded, Enum)
instance Show AnnotationTarget where
show FileAnnotation = "file"
show ConstantAnnotation = "const"
show EnumAnnotation = "enum"
show EnumValueAnnotation = "enumerant"
show StructAnnotation = "struct"
show FieldAnnotation = "field"
show UnionAnnotation = "union"
show InterfaceAnnotation = "interface"
show MethodAnnotation = "method"
show ParamAnnotation = "param"
show AnnotationAnnotation = "annotation"
data Declaration = AliasDecl (Located String) DeclName data Declaration = AliasDecl (Located String) DeclName
| ConstantDecl (Located String) TypeExpression (Located FieldValue) | ConstantDecl (Located String) TypeExpression [Annotation] (Located FieldValue)
| EnumDecl (Located String) [Declaration] | EnumDecl (Located String) [Annotation] [Declaration]
| EnumValueDecl (Located String) (Located Integer) [Declaration] | EnumValueDecl (Located String) (Located Integer) [Annotation]
| StructDecl (Located String) [Declaration] | StructDecl (Located String) [Annotation] [Declaration]
| FieldDecl (Located String) (Located Integer) | FieldDecl (Located String) (Located Integer)
TypeExpression (Maybe (Located FieldValue)) TypeExpression [Annotation] (Maybe (Located FieldValue))
| UnionDecl (Located String) (Located Integer) [Declaration] | UnionDecl (Located String) (Located Integer) [Annotation] [Declaration]
| InterfaceDecl (Located String) [Declaration] | InterfaceDecl (Located String) [Annotation] [Declaration]
| MethodDecl (Located String) (Located Integer) | MethodDecl (Located String) (Located Integer) [ParamDecl]
[(String, TypeExpression, Maybe (Located FieldValue))] TypeExpression [Annotation]
TypeExpression [Declaration] | AnnotationDecl (Located String) TypeExpression [Annotation] [AnnotationTarget]
| OptionDecl DeclName (Located FieldValue)
deriving (Show) deriving (Show)
declarationName :: Declaration -> Maybe (Located String) declarationName :: Declaration -> Maybe (Located String)
declarationName (AliasDecl n _) = Just n declarationName (AliasDecl n _) = Just n
declarationName (ConstantDecl n _ _) = Just n declarationName (ConstantDecl n _ _ _) = Just n
declarationName (EnumDecl n _) = Just n declarationName (EnumDecl n _ _) = Just n
declarationName (EnumValueDecl n _ _) = Just n declarationName (EnumValueDecl n _ _) = Just n
declarationName (StructDecl n _) = Just n declarationName (StructDecl n _ _) = Just n
declarationName (FieldDecl n _ _ _) = Just n declarationName (FieldDecl n _ _ _ _) = Just n
declarationName (UnionDecl n _ _) = Just n declarationName (UnionDecl n _ _ _) = Just n
declarationName (InterfaceDecl n _) = Just n declarationName (InterfaceDecl n _ _) = Just n
declarationName (MethodDecl n _ _ _ _) = Just n declarationName (MethodDecl n _ _ _ _) = Just n
declarationName (OptionDecl _ _) = Nothing declarationName (AnnotationDecl n _ _ _) = Just n
declImports :: Declaration -> [Located String] declImports :: Declaration -> [Located String]
declImports (AliasDecl _ name) = maybeToList $ declNameImport name declImports (AliasDecl _ name) = maybeToList (declNameImport name)
declImports (ConstantDecl _ t _) = typeImports t declImports (ConstantDecl _ t ann _) = typeImports t ++ concatMap annotationImports ann
declImports (EnumDecl _ decls) = concatMap declImports decls declImports (EnumDecl _ ann decls) = concatMap annotationImports ann ++ concatMap declImports decls
declImports (EnumValueDecl _ _ decls) = concatMap declImports decls declImports (EnumValueDecl _ _ ann) = concatMap annotationImports ann
declImports (StructDecl _ decls) = concatMap declImports decls declImports (StructDecl _ ann decls) = concatMap annotationImports ann ++
declImports (FieldDecl _ _ t _) = typeImports t concatMap declImports decls
declImports (UnionDecl _ _ decls) = concatMap declImports decls declImports (FieldDecl _ _ t ann _) = typeImports t ++ concatMap annotationImports ann
declImports (InterfaceDecl _ decls) = concatMap declImports decls declImports (UnionDecl _ _ ann decls) = concatMap annotationImports ann ++
declImports (MethodDecl _ _ params t decls) = concatMap declImports decls
concat [paramsImports, typeImports t, concatMap declImports decls] where declImports (InterfaceDecl _ ann decls) = concatMap annotationImports ann ++
paramsImports = concat [typeImports pt | (_, pt, _) <- params] concatMap declImports decls
declImports (OptionDecl name _) = maybeToList $ declNameImport name declImports (MethodDecl _ _ params t ann) =
concat [concatMap paramImports params, typeImports t, concatMap annotationImports ann]
declImports (AnnotationDecl _ t ann _) = typeImports t ++ concatMap annotationImports ann
...@@ -48,7 +48,7 @@ keywords = ...@@ -48,7 +48,7 @@ keywords =
, (StructKeyword, "struct") , (StructKeyword, "struct")
, (UnionKeyword, "union") , (UnionKeyword, "union")
, (InterfaceKeyword, "interface") , (InterfaceKeyword, "interface")
, (OptionKeyword, "option") , (AnnotationKeyword, "annotation")
] ]
languageDef :: T.LanguageDef st languageDef :: T.LanguageDef st
...@@ -120,9 +120,11 @@ token = keyword ...@@ -120,9 +120,11 @@ token = keyword
<|> liftM LiteralString stringLiteral <|> liftM LiteralString stringLiteral
<|> liftM (const AtSign) (symbol "@") <|> liftM (const AtSign) (symbol "@")
<|> liftM (const Colon) (symbol ":") <|> liftM (const Colon) (symbol ":")
<|> liftM (const DollarSign) (symbol "$")
<|> liftM (const Period) (symbol ".") <|> liftM (const Period) (symbol ".")
<|> liftM (const EqualsSign) (symbol "=") <|> liftM (const EqualsSign) (symbol "=")
<|> liftM (const MinusSign) (symbol "-") <|> liftM (const MinusSign) (symbol "-")
<|> liftM (const Asterisk) (symbol "*")
<|> liftM (const ExclamationPoint) (symbol "!") <|> liftM (const ExclamationPoint) (symbol "!")
<?> "token" <?> "token"
......
This diff is collapsed.
This diff is collapsed.
...@@ -54,9 +54,11 @@ data Token = Identifier String ...@@ -54,9 +54,11 @@ data Token = Identifier String
| FalseKeyword | FalseKeyword
| AtSign | AtSign
| Colon | Colon
| DollarSign
| Period | Period
| EqualsSign | EqualsSign
| MinusSign | MinusSign
| Asterisk
| ExclamationPoint | ExclamationPoint
| InKeyword | InKeyword
| OfKeyword -- We reserve some common, short English words for use as future keywords. | OfKeyword -- We reserve some common, short English words for use as future keywords.
...@@ -71,7 +73,7 @@ data Token = Identifier String ...@@ -71,7 +73,7 @@ data Token = Identifier String
| StructKeyword | StructKeyword
| UnionKeyword | UnionKeyword
| InterfaceKeyword | InterfaceKeyword
| OptionKeyword | AnnotationKeyword
deriving (Data, Typeable, Show, Eq) deriving (Data, Typeable, Show, Eq)
data Statement = Line TokenSequence data Statement = Line TokenSequence
......
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