Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
22a75445
Commit
22a75445
authored
Feb 16, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Compile semantic descriptors.
parent
6bb49ca7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
75 additions
and
54 deletions
+75
-54
capnproto-compiler.cabal
compiler/capnproto-compiler.cabal
+3
-1
Compiler.hs
compiler/src/Compiler.hs
+0
-0
Grammar.hs
compiler/src/Grammar.hs
+18
-14
Lexer.hs
compiler/src/Lexer.hs
+1
-0
Main.hs
compiler/src/Main.hs
+13
-1
Parser.hs
compiler/src/Parser.hs
+39
-27
Semantics.hs
compiler/src/Semantics.hs
+0
-0
Token.hs
compiler/src/Token.hs
+1
-11
No files found.
compiler/capnproto-compiler.cabal
View file @
22a75445
...
@@ -17,5 +17,7 @@ executable capnproto-compiler
...
@@ -17,5 +17,7 @@ executable capnproto-compiler
Lexer,
Lexer,
Token,
Token,
Grammar,
Grammar,
Parser
Parser,
Compiler,
Semantics
compiler/src/Compiler.hs
0 → 100644
View file @
22a75445
This diff is collapsed.
Click to expand it.
compiler/src/Grammar.hs
View file @
22a75445
...
@@ -23,14 +23,15 @@
...
@@ -23,14 +23,15 @@
module
Grammar
where
module
Grammar
where
data
DeclName
=
AbsoluteName
String
import
Token
(
Located
)
|
RelativeName
String
|
ImportName
String
data
DeclName
=
AbsoluteName
(
Located
String
)
|
MemberName
DeclName
String
|
RelativeName
(
Located
String
)
|
ImportName
(
Located
String
)
|
MemberName
DeclName
(
Located
String
)
deriving
(
Show
)
deriving
(
Show
)
data
TypeExpression
=
TypeName
DeclName
data
TypeExpression
=
TypeExpression
DeclName
[
TypeExpression
]
|
Array
TypeExpression
deriving
(
Show
)
deriving
(
Show
)
data
FieldValue
=
VoidFieldValue
data
FieldValue
=
VoidFieldValue
...
@@ -42,13 +43,16 @@ data FieldValue = VoidFieldValue
...
@@ -42,13 +43,16 @@ data FieldValue = VoidFieldValue
|
RecordFieldValue
[(
String
,
FieldValue
)]
|
RecordFieldValue
[(
String
,
FieldValue
)]
deriving
(
Show
)
deriving
(
Show
)
data
Declaration
=
ConstantDecl
String
(
Maybe
TypeExpression
)
FieldValue
data
Declaration
=
AliasDecl
(
Located
String
)
DeclName
|
EnumDecl
String
[
Declaration
]
|
ConstantDecl
(
Located
String
)
TypeExpression
(
Located
FieldValue
)
|
EnumValueDecl
String
Integer
[
Declaration
]
|
EnumDecl
(
Located
String
)
[
Declaration
]
|
ClassDecl
String
[
Declaration
]
|
EnumValueDecl
(
Located
String
)
(
Located
Integer
)
[
Declaration
]
|
FieldDecl
String
Integer
TypeExpression
FieldValue
[
Declaration
]
|
ClassDecl
(
Located
String
)
[
Declaration
]
|
InterfaceDecl
String
[
Declaration
]
|
FieldDecl
(
Located
String
)
(
Located
Integer
)
|
MethodDecl
String
[(
String
,
TypeExpression
,
FieldValue
)]
TypeExpression
(
Maybe
(
Located
FieldValue
))
[
Declaration
]
|
InterfaceDecl
(
Located
String
)
[
Declaration
]
|
MethodDecl
(
Located
String
)
(
Located
Integer
)
[(
String
,
TypeExpression
,
Maybe
(
Located
FieldValue
))]
TypeExpression
[
Declaration
]
TypeExpression
[
Declaration
]
|
OptionDecl
DeclName
FieldValue
|
OptionDecl
DeclName
(
Located
FieldValue
)
deriving
(
Show
)
deriving
(
Show
)
compiler/src/Lexer.hs
View file @
22a75445
...
@@ -32,6 +32,7 @@ import Token
...
@@ -32,6 +32,7 @@ import Token
keywords
=
keywords
=
[
(
ImportKeyword
,
"import"
)
[
(
ImportKeyword
,
"import"
)
,
(
UsingKeyword
,
"using"
)
,
(
ConstKeyword
,
"const"
)
,
(
ConstKeyword
,
"const"
)
,
(
EnumKeyword
,
"enum"
)
,
(
EnumKeyword
,
"enum"
)
,
(
ClassKeyword
,
"class"
)
,
(
ClassKeyword
,
"class"
)
...
...
compiler/src/Main.hs
View file @
22a75445
...
@@ -23,5 +23,17 @@
...
@@ -23,5 +23,17 @@
module
Main
(
main
)
where
module
Main
(
main
)
where
import
System.Environment
import
Compiler
main
::
IO
()
main
::
IO
()
main
=
undefined
main
=
do
files
<-
getArgs
mapM_
handleFile
files
handleFile
filename
=
do
text
<-
readFile
filename
case
parseAndCompileFile
filename
text
of
Active
desc
[]
->
print
desc
Active
_
e
->
mapM_
print
e
Failed
e
->
mapM_
print
e
compiler/src/Parser.hs
View file @
22a75445
...
@@ -34,6 +34,12 @@ tokenParser = token (show . locatedValue) locatedPos
...
@@ -34,6 +34,12 @@ tokenParser = token (show . locatedValue) locatedPos
type
TokenParser
=
Parsec
[
Located
Token
]
[
ParseError
]
type
TokenParser
=
Parsec
[
Located
Token
]
[
ParseError
]
located
::
TokenParser
t
->
TokenParser
(
Located
t
)
located
p
=
do
input
<-
getInput
t
<-
p
return
(
Located
(
locatedPos
(
head
input
))
t
)
-- Hmm, boilerplate is not supposed to happen in Haskell.
-- Hmm, boilerplate is not supposed to happen in Haskell.
matchIdentifier
t
=
case
locatedValue
t
of
{
(
Identifier
v
)
->
Just
v
;
_
->
Nothing
}
matchIdentifier
t
=
case
locatedValue
t
of
{
(
Identifier
v
)
->
Just
v
;
_
->
Nothing
}
matchParenthesizedList
t
=
case
locatedValue
t
of
{
(
ParenthesizedList
v
)
->
Just
v
;
_
->
Nothing
}
matchParenthesizedList
t
=
case
locatedValue
t
of
{
(
ParenthesizedList
v
)
->
Just
v
;
_
->
Nothing
}
...
@@ -53,6 +59,7 @@ colon = tokenParser (matchSimpleToken Colon)
...
@@ -53,6 +59,7 @@ colon = tokenParser (matchSimpleToken Colon)
period
=
tokenParser
(
matchSimpleToken
Period
)
period
=
tokenParser
(
matchSimpleToken
Period
)
equalsSign
=
tokenParser
(
matchSimpleToken
EqualsSign
)
equalsSign
=
tokenParser
(
matchSimpleToken
EqualsSign
)
importKeyword
=
tokenParser
(
matchSimpleToken
ImportKeyword
)
importKeyword
=
tokenParser
(
matchSimpleToken
ImportKeyword
)
usingKeyword
=
tokenParser
(
matchSimpleToken
UsingKeyword
)
constKeyword
=
tokenParser
(
matchSimpleToken
ConstKeyword
)
constKeyword
=
tokenParser
(
matchSimpleToken
ConstKeyword
)
enumKeyword
=
tokenParser
(
matchSimpleToken
EnumKeyword
)
enumKeyword
=
tokenParser
(
matchSimpleToken
EnumKeyword
)
classKeyword
=
tokenParser
(
matchSimpleToken
ClassKeyword
)
classKeyword
=
tokenParser
(
matchSimpleToken
ClassKeyword
)
...
@@ -67,37 +74,40 @@ bracketedList parser = do
...
@@ -67,37 +74,40 @@ bracketedList parser = do
parseList
parser
items
parseList
parser
items
declNameBase
::
TokenParser
DeclName
declNameBase
::
TokenParser
DeclName
declNameBase
=
liftM
ImportName
(
importKeyword
>>
literalString
)
declNameBase
=
liftM
ImportName
(
importKeyword
>>
l
ocated
l
iteralString
)
<|>
liftM
AbsoluteName
(
period
>>
identifier
)
<|>
liftM
AbsoluteName
(
period
>>
located
identifier
)
<|>
liftM
RelativeName
identifier
<|>
liftM
RelativeName
(
located
identifier
)
declName
::
TokenParser
DeclName
declName
::
TokenParser
DeclName
declName
=
do
declName
=
do
base
<-
declNameBase
base
<-
declNameBase
members
<-
many
(
period
>>
identifier
)
members
<-
many
(
period
>>
located
identifier
)
return
(
foldl
MemberName
base
members
::
DeclName
)
return
(
foldl
MemberName
base
members
::
DeclName
)
typeExpression
::
TokenParser
TypeExpression
typeExpression
::
TokenParser
TypeExpression
typeExpression
=
do
typeExpression
=
do
name
<-
declName
name
<-
declName
suffixes
<-
many
(
bracketedList
(
fail
"Brackets should be empty."
))
suffixes
<-
option
[]
(
parenthesizedList
typeExpression
)
return
(
applySuffixes
(
TypeName
name
)
(
length
suffixes
))
where
return
(
TypeExpression
name
suffixes
)
applySuffixes
t
0
=
t
applySuffixes
t
n
=
applySuffixes
(
Array
t
)
(
n
-
1
)
topLine
::
Maybe
[
Located
Statement
]
->
TokenParser
Declaration
topLine
::
Maybe
[
Located
Statement
]
->
TokenParser
Declaration
topLine
Nothing
=
optionDecl
<|>
constantDecl
<|>
implicitC
onstantDecl
topLine
Nothing
=
optionDecl
<|>
aliasDecl
<|>
c
onstantDecl
topLine
(
Just
statements
)
=
typeDecl
statements
topLine
(
Just
statements
)
=
typeDecl
statements
aliasDecl
=
do
usingKeyword
name
<-
located
identifier
equalsSign
target
<-
declName
return
(
AliasDecl
name
target
)
constantDecl
=
do
constantDecl
=
do
constKeyword
constKeyword
implicitConstantDecl
name
<-
located
identifier
colon
implicitConstantDecl
=
do
typeName
<-
typeExpression
name
<-
identifier
typeName
<-
optionMaybe
(
period
>>
typeExpression
)
equalsSign
equalsSign
value
<-
fieldValue
value
<-
located
fieldValue
return
(
ConstantDecl
name
typeName
value
)
return
(
ConstantDecl
name
typeName
value
)
typeDecl
statements
=
enumDecl
statements
typeDecl
statements
=
enumDecl
statements
...
@@ -106,7 +116,7 @@ typeDecl statements = enumDecl statements
...
@@ -106,7 +116,7 @@ typeDecl statements = enumDecl statements
enumDecl
statements
=
do
enumDecl
statements
=
do
enumKeyword
enumKeyword
name
<-
identifier
name
<-
located
identifier
children
<-
parseBlock
enumLine
statements
children
<-
parseBlock
enumLine
statements
return
(
EnumDecl
name
children
)
return
(
EnumDecl
name
children
)
...
@@ -115,9 +125,9 @@ enumLine Nothing = optionDecl <|> enumValueDecl []
...
@@ -115,9 +125,9 @@ enumLine Nothing = optionDecl <|> enumValueDecl []
enumLine
(
Just
statements
)
=
enumValueDecl
statements
enumLine
(
Just
statements
)
=
enumValueDecl
statements
enumValueDecl
statements
=
do
enumValueDecl
statements
=
do
name
<-
identifier
name
<-
located
identifier
equalsSign
equalsSign
value
<-
literalInt
value
<-
l
ocated
l
iteralInt
children
<-
parseBlock
enumValueLine
statements
children
<-
parseBlock
enumValueLine
statements
return
(
EnumValueDecl
name
value
children
)
return
(
EnumValueDecl
name
value
children
)
...
@@ -127,7 +137,7 @@ enumValueLine (Just _) = fail "Blocks not allowed here."
...
@@ -127,7 +137,7 @@ enumValueLine (Just _) = fail "Blocks not allowed here."
classDecl
statements
=
do
classDecl
statements
=
do
classKeyword
classKeyword
name
<-
identifier
name
<-
located
identifier
children
<-
parseBlock
classLine
statements
children
<-
parseBlock
classLine
statements
return
(
ClassDecl
name
children
)
return
(
ClassDecl
name
children
)
...
@@ -136,12 +146,12 @@ classLine Nothing = optionDecl <|> constantDecl <|> fieldDecl []
...
@@ -136,12 +146,12 @@ classLine Nothing = optionDecl <|> constantDecl <|> fieldDecl []
classLine
(
Just
statements
)
=
typeDecl
statements
<|>
fieldDecl
statements
classLine
(
Just
statements
)
=
typeDecl
statements
<|>
fieldDecl
statements
fieldDecl
statements
=
do
fieldDecl
statements
=
do
name
<-
identifier
name
<-
located
identifier
atSign
atSign
ordinal
<-
literalInt
ordinal
<-
l
ocated
l
iteralInt
colon
colon
t
<-
typeExpression
t
<-
typeExpression
value
<-
option
VoidFieldValue
(
equalsSign
>>
fieldValue
)
value
<-
option
Maybe
(
equalsSign
>>
located
fieldValue
)
children
<-
parseBlock
fieldLine
statements
children
<-
parseBlock
fieldLine
statements
return
(
FieldDecl
name
ordinal
t
value
children
)
return
(
FieldDecl
name
ordinal
t
value
children
)
...
@@ -163,7 +173,7 @@ fieldLine (Just _) = fail "Blocks not allowed here."
...
@@ -163,7 +173,7 @@ fieldLine (Just _) = fail "Blocks not allowed here."
interfaceDecl
statements
=
do
interfaceDecl
statements
=
do
interfaceKeyword
interfaceKeyword
name
<-
identifier
name
<-
located
identifier
children
<-
parseBlock
interfaceLine
statements
children
<-
parseBlock
interfaceLine
statements
return
(
InterfaceDecl
name
children
)
return
(
InterfaceDecl
name
children
)
...
@@ -172,17 +182,19 @@ interfaceLine Nothing = optionDecl <|> constantDecl <|> methodDecl []
...
@@ -172,17 +182,19 @@ interfaceLine Nothing = optionDecl <|> constantDecl <|> methodDecl []
interfaceLine
(
Just
statements
)
=
typeDecl
statements
<|>
methodDecl
statements
interfaceLine
(
Just
statements
)
=
typeDecl
statements
<|>
methodDecl
statements
methodDecl
statements
=
do
methodDecl
statements
=
do
name
<-
identifier
name
<-
located
identifier
atSign
ordinal
<-
located
literalInt
params
<-
parenthesizedList
paramDecl
params
<-
parenthesizedList
paramDecl
t
<-
typeExpression
t
<-
typeExpression
children
<-
parseBlock
methodLine
statements
children
<-
parseBlock
methodLine
statements
return
(
MethodDecl
name
params
t
children
)
return
(
MethodDecl
name
ordinal
params
t
children
)
paramDecl
=
do
paramDecl
=
do
name
<-
identifier
name
<-
identifier
colon
colon
t
<-
typeExpression
t
<-
typeExpression
value
<-
option
VoidFieldValue
(
equalsSign
>>
fieldValue
)
value
<-
option
Maybe
(
equalsSign
>>
located
fieldValue
)
return
(
name
,
t
,
value
)
return
(
name
,
t
,
value
)
methodLine
::
Maybe
[
Located
Statement
]
->
TokenParser
Declaration
methodLine
::
Maybe
[
Located
Statement
]
->
TokenParser
Declaration
...
@@ -193,7 +205,7 @@ optionDecl = do
...
@@ -193,7 +205,7 @@ optionDecl = do
optionKeyword
optionKeyword
name
<-
declName
name
<-
declName
equalsSign
equalsSign
value
<-
fieldValue
value
<-
located
fieldValue
return
(
OptionDecl
name
value
)
return
(
OptionDecl
name
value
)
extractErrors
::
Either
ParseError
(
a
,
[
ParseError
])
->
[
ParseError
]
extractErrors
::
Either
ParseError
(
a
,
[
ParseError
])
->
[
ParseError
]
...
...
compiler/src/Semantics.hs
0 → 100644
View file @
22a75445
This diff is collapsed.
Click to expand it.
compiler/src/Token.hs
View file @
22a75445
...
@@ -23,20 +23,9 @@
...
@@ -23,20 +23,9 @@
module
Token
where
module
Token
where
import
Data.Char
(
toLower
)
import
Text.Parsec.Pos
(
SourcePos
,
sourceLine
,
sourceColumn
)
import
Text.Parsec.Pos
(
SourcePos
,
sourceLine
,
sourceColumn
)
import
Text.Printf
(
printf
)
import
Text.Printf
(
printf
)
data
PrimitiveType
=
Void
|
Bool
|
Int8
|
Int16
|
Int32
|
Int64
|
UInt8
|
UInt16
|
UInt32
|
UInt64
|
Float32
|
Float64
|
Text
|
Bytes
deriving
(
Show
,
Enum
,
Bounded
,
Eq
)
primitiveTypes
=
[(
t
,
map
toLower
(
show
t
))
|
t
<-
[
minBound
::
PrimitiveType
..
maxBound
::
PrimitiveType
]]
data
Located
t
=
Located
{
locatedPos
::
SourcePos
,
locatedValue
::
t
}
deriving
(
Eq
)
data
Located
t
=
Located
{
locatedPos
::
SourcePos
,
locatedValue
::
t
}
deriving
(
Eq
)
instance
Show
t
=>
Show
(
Located
t
)
where
instance
Show
t
=>
Show
(
Located
t
)
where
...
@@ -53,6 +42,7 @@ data Token = Identifier String
...
@@ -53,6 +42,7 @@ data Token = Identifier String
|
Period
|
Period
|
EqualsSign
|
EqualsSign
|
ImportKeyword
|
ImportKeyword
|
UsingKeyword
|
ConstKeyword
|
ConstKeyword
|
EnumKeyword
|
EnumKeyword
|
ClassKeyword
|
ClassKeyword
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment