Skip to content

Commit

Permalink
Rename boxed to unboxed. Fix nirum-lang#65
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Sep 23, 2016
1 parent 8178596 commit 0a6bfb5
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 165 deletions.
20 changes: 10 additions & 10 deletions docs/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ It's represented in JSON to:
}


Boxed type
----------
Unboxed type
------------

Boxed type is equivalent to 1-member record type in runtime, but it's equivalent
to its internal type in JSON representation. For example:
Unboxed type is equivalent to 1-member record type in runtime,
but it's equivalent to its internal type in JSON representation. For example:

boxed offset (float64);
unboxed offset (float64);

record payload (
offset left,
Expand All @@ -112,7 +112,7 @@ The internal type might be a record type as well:
float64 top,
);

boxed coord (point);
unboxed coord (point);

record payload (
coord location,
Expand All @@ -131,14 +131,14 @@ It's represented in JSON as:

The internal type also can be a option/set/list/map type:

boxed box-option (text?);
unboxed box-option (text?);

enum color = red | green | blue;
boxed box-set ({color});
unboxed box-set ({color});

boxed box-list ([float64]);
unboxed box-list ([float64]);

boxed box-map ({uuid: datetime});
unboxed box-map ({uuid: datetime});

record payload (
box-option a,
Expand Down
6 changes: 3 additions & 3 deletions examples/shapes.nrm
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Module consists zero or more type declarations.

boxed offset (float64);
# The key difference between boxed type and reocrd type consisting of a single
unboxed offset (float64);
# The key difference between unboxed type and reocrd type consisting of a single
# field is that the former has the same JSON representation to its target type,
# while the latter has thicker JSON representation than its only field.
#
# For example, a value of `boxed offset (float);` type is represented
# For example, a value of `unboxed offset (float);` type is represented
# in JSON as:
#
# 120.5
Expand Down
4 changes: 2 additions & 2 deletions src/Nirum/Constructs/Identifier.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ import Nirum.Constructs (Construct(toCode))
data Identifier = Identifier T.Text deriving (Show)

reservedKeywords :: S.Set Identifier
reservedKeywords = [ "boxed"
, "enum"
reservedKeywords = [ "enum"
, "record"
, "service"
, "throws"
, "type"
, "unboxed"
, "union"
]

Expand Down
6 changes: 3 additions & 3 deletions src/Nirum/Constructs/TypeDeclaration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import Nirum.Constructs.TypeExpression (TypeExpression)

data Type
= Alias { canonicalType :: TypeExpression }
| BoxedType { innerType :: TypeExpression }
| UnboxedType { innerType :: TypeExpression }
| EnumType { members :: DeclarationSet EnumMember }
| RecordType { fields :: DeclarationSet Field }
| UnionType { tags :: DeclarationSet Tag }
Expand Down Expand Up @@ -143,9 +143,9 @@ instance Construct TypeDeclaration where
, " = ", toCode cname, ";"
, toCodeWithPrefix "\n" (A.lookupDocs annotationSet')
]
toCode (TypeDeclaration name' (BoxedType itype) annotationSet') =
toCode (TypeDeclaration name' (UnboxedType itype) annotationSet') =
T.concat [ toCode annotationSet'
, "boxed ", toCode name'
, "unboxed ", toCode name'
, " (", toCode itype, ");"
, toCodeWithPrefix "\n" (A.lookupDocs annotationSet')]
toCode (TypeDeclaration name' (EnumType members') annotationSet') =
Expand Down
26 changes: 13 additions & 13 deletions src/Nirum/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Nirum.Parser ( Parser
, aliasTypeDeclaration
, annotation
, annotationSet
, boxedTypeDeclaration
, docs
, enumTypeDeclaration
, file
Expand All @@ -28,6 +27,7 @@ module Nirum.Parser ( Parser
, typeExpression
, typeExpressionWithoutOptionModifier
, typeIdentifier
, unboxedTypeDeclaration
, unionTypeDeclaration
) where

Expand Down Expand Up @@ -94,9 +94,9 @@ import Nirum.Constructs.TypeDeclaration ( EnumMember(EnumMember)
, Field(Field)
, Tag(Tag)
, Type( Alias
, BoxedType
, EnumType
, RecordType
, UnboxedType
, UnionType
)
, TypeDeclaration( Import
Expand Down Expand Up @@ -266,24 +266,24 @@ aliasTypeDeclaration = do
return $ TypeDeclaration name' (Alias canonicalType) annotationSet''


boxedTypeDeclaration :: Parser TypeDeclaration
boxedTypeDeclaration = do
annotationSet' <- annotationSet <?> "boxed type annotations"
string' "boxed" <?> "boxed type keyword"
unboxedTypeDeclaration :: Parser TypeDeclaration
unboxedTypeDeclaration = do
annotationSet' <- annotationSet <?> "unboxed type annotations"
string' "unboxed" <?> "unboxed type keyword"
spaces
typename <- identifier <?> "boxed type name"
typename <- identifier <?> "unboxed type name"
let name' = Name typename typename
spaces
char '('
spaces
innerType <- typeExpression <?> "inner type of boxed type"
innerType <- typeExpression <?> "inner type of unboxed type"
spaces
char ')'
spaces
char ';'
docs' <- optional $ try $ spaces >> (docs <?> "boxed type docs")
docs' <- optional $ try $ spaces >> (docs <?> "unboxed type docs")
annotationSet'' <- annotationsWithDocs annotationSet' docs'
return $ TypeDeclaration name' (BoxedType innerType) annotationSet''
return $ TypeDeclaration name' (UnboxedType innerType) annotationSet''

enumMember :: Parser EnumMember
enumMember = do
Expand Down Expand Up @@ -462,12 +462,12 @@ typeDeclaration = do
annotationSet' <- annotationSet <?> "type annotations"
spaces
typeDecl <- choice
[ unless' ["union", "record", "enum", "boxed"] aliasTypeDeclaration
, unless' ["union", "record", "enum"] boxedTypeDeclaration
[ unless' ["union", "record", "enum", "unboxed"] aliasTypeDeclaration
, unless' ["union", "record", "enum"] unboxedTypeDeclaration
, unless' ["union", "record"] enumTypeDeclaration
, unless' ["union"] recordTypeDeclaration
, unionTypeDeclaration
] <?> "type declaration (e.g. boxed, enum, record, union)"
] <?> "type declaration (e.g. enum, record, unboxed, union)"
-- In theory, though it preconsumes annotationSet' before parsing typeDecl
-- so that typeDecl itself has no annotations, to prepare for an
-- unlikely situation (that I bet it'll never happen)
Expand Down
21 changes: 11 additions & 10 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ import Nirum.Constructs.TypeDeclaration ( EnumMember(EnumMember)
, PrimitiveTypeIdentifier(..)
, Tag(Tag)
, Type( Alias
, BoxedType
, EnumType
, PrimitiveType
, RecordType
, UnboxedType
, UnionType
)
, TypeDeclaration(..)
Expand Down Expand Up @@ -311,22 +311,23 @@ compileTypeDeclaration src TypeDeclaration { typename = typename'
{toClassName' typename'} = $ctypeExpr
|]
compileTypeDeclaration src TypeDeclaration { typename = typename'
, type' = BoxedType itype } = do
, type' = UnboxedType itype } = do
let className = toClassName' typename'
itypeExpr <- compileTypeExpression src itype
insertStandardImport "typing"
insertThirdPartyImports [ ("nirum.validate", ["validate_boxed_type"])
, ("nirum.serialize", ["serialize_boxed_type"])
, ("nirum.deserialize", ["deserialize_boxed_type"])
]
insertThirdPartyImports
[ ("nirum.validate", ["validate_unboxed_type"])
, ("nirum.serialize", ["serialize_unboxed_type"])
, ("nirum.deserialize", ["deserialize_unboxed_type"])
]
return [qq|
class $className:
# TODO: docstring

__nirum_boxed_type__ = $itypeExpr
__nirum_inner_type__ = $itypeExpr

def __init__(self, value: $itypeExpr) -> None:
validate_boxed_type(value, $itypeExpr)
validate_unboxed_type(value, $itypeExpr)
self.value = value # type: $itypeExpr

def __eq__(self, other) -> bool:
Expand All @@ -337,11 +338,11 @@ class $className:
return hash(self.value)

def __nirum_serialize__(self) -> typing.Any:
return serialize_boxed_type(self)
return serialize_unboxed_type(self)

@classmethod
def __nirum_deserialize__(cls: type, value: typing.Any) -> '{className}':
return deserialize_boxed_type(cls, value)
return deserialize_unboxed_type(cls, value)

def __repr__(self) -> str:
return '\{0.__module__\}.\{0.__qualname__\}(\{1!r\})'.format(
Expand Down
6 changes: 3 additions & 3 deletions test/Nirum/Constructs/ModuleSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spec =
let docsAnno = A.docs "path string"
pathT = TypeDeclaration "path" (Alias "text") (singleton docsAnno)
offsetT =
TypeDeclaration "offset" (BoxedType "float64") empty
TypeDeclaration "offset" (UnboxedType "float64") empty
decls = [ Import ["foo", "bar"] "baz" empty
, Import ["foo", "bar"] "qux" empty
, Import ["zzz"] "qqq" empty
Expand All @@ -45,7 +45,7 @@ import zzz (ppp, qqq);
type path = text;
# path string

boxed offset (float64);
unboxed offset (float64);
|]
toCode mod2 `shouldBe` [q|# module level docs...
# blahblah
Expand All @@ -56,5 +56,5 @@ import zzz (ppp, qqq);
type path = text;
# path string

boxed offset (float64);
unboxed offset (float64);
|]
10 changes: 5 additions & 5 deletions test/Nirum/Constructs/TypeDeclarationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ spec = do
specify "toCode" $ do
toCode a `shouldBe` "type path = text;"
toCode b `shouldBe` "type path = text;\n# docs"
context "BoxedType" $ do
let boxed = BoxedType "float64"
context "UnboxedType" $ do
let unboxed = UnboxedType "float64"
a = TypeDeclaration { typename = "offset"
, type' = boxed
, type' = unboxed
, typeAnnotations = empty
}
b = a { typeAnnotations = singleDocs "docs" }
Expand All @@ -60,8 +60,8 @@ spec = do
docs a `shouldBe` Nothing
docs b `shouldBe` Just "docs"
specify "toCode" $ do
toCode a `shouldBe` "boxed offset (float64);"
toCode b `shouldBe` "boxed offset (float64);\n# docs"
toCode a `shouldBe` "unboxed offset (float64);"
toCode b `shouldBe` "unboxed offset (float64);\n# docs"
context "EnumType" $ do
let enumMembers = [ EnumMember "kr" empty
, EnumMember "jp" (singleDocs "Japan")
Expand Down
Loading

0 comments on commit 0a6bfb5

Please sign in to comment.