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
6819f272
Commit
6819f272
authored
Apr 16, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'scrap-parser-boilerplate' of
https://github.com/alekstorm/capnproto
parents
2c4c98e3
f60e7ce0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
19 deletions
+23
-19
capnproto-compiler.cabal
compiler/capnproto-compiler.cabal
+3
-2
Parser.hs
compiler/src/Parser.hs
+14
-14
Token.hs
compiler/src/Token.hs
+6
-3
No files found.
compiler/capnproto-compiler.cabal
View file @
6819f272
...
...
@@ -20,7 +20,8 @@ executable capnpc
array,
data-binary-ieee754,
filepath,
directory
directory,
syb
ghc-options: -Wall -fno-warn-missing-signatures
other-modules:
Lexer,
...
...
@@ -31,5 +32,5 @@ executable capnpc
Semantics,
Util,
CxxGenerator,
JsonGenerator,
WireFormat
compiler/src/Parser.hs
View file @
6819f272
...
...
@@ -23,6 +23,7 @@
module
Parser
(
parseFile
)
where
import
Data.Generics
import
Text.Parsec
hiding
(
tokens
)
import
Text.Parsec.Error
(
newErrorMessage
,
Message
(
Message
))
import
Token
...
...
@@ -72,14 +73,13 @@ located p = do
t
<-
p
return
(
Located
(
locatedPos
(
head
input
))
t
)
-- Hmm, boilerplate is not supposed to happen in Haskell.
matchIdentifier
t
=
case
locatedValue
t
of
{
(
Identifier
v
)
->
Just
v
;
_
->
Nothing
}
matchTypeIdentifier
t
=
case
locatedValue
t
of
{
(
TypeIdentifier
v
)
->
Just
v
;
_
->
Nothing
}
matchParenthesizedList
t
=
case
locatedValue
t
of
{
(
ParenthesizedList
v
)
->
Just
v
;
_
->
Nothing
}
matchBracketedList
t
=
case
locatedValue
t
of
{
(
BracketedList
v
)
->
Just
v
;
_
->
Nothing
}
matchLiteralInt
t
=
case
locatedValue
t
of
{
(
LiteralInt
v
)
->
Just
v
;
_
->
Nothing
}
matchLiteralFloat
t
=
case
locatedValue
t
of
{
(
LiteralFloat
v
)
->
Just
v
;
_
->
Nothing
}
matchLiteralString
t
=
case
locatedValue
t
of
{
(
LiteralString
v
)
->
Just
v
;
_
->
Nothing
}
matchUnary
::
(
Data
a
,
Data
b
)
=>
(
a
->
b
)
->
Located
b
->
Maybe
a
matchUnary
c
t
=
if
toConstr
(
c
undefined
)
==
toConstr
(
v
)
then
Just
$
gmapQi
0
(
undefined
`
mkQ
`
id
)
v
else
Nothing
where
v
=
locatedValue
t
matchIdentifier
=
matchUnary
Identifier
matchTypeIdentifier
=
matchUnary
TypeIdentifier
matchLiteralBool
t
=
case
locatedValue
t
of
TrueKeyword
->
Just
True
FalseKeyword
->
Just
False
...
...
@@ -98,10 +98,10 @@ anyIdentifier = tokenParser matchIdentifier
<|>
tokenParser
matchTypeIdentifier
<?>
"identifier"
literalInt
=
tokenParser
matchLiteralInt
<?>
"integer"
literalFloat
=
tokenParser
matchLiteralFloat
<?>
"floating-point number"
literalString
=
tokenParser
matchLiteralString
<?>
"string"
literalBool
=
tokenParser
matchLiteralBool
<?>
"boolean"
literalInt
=
tokenParser
(
matchUnary
LiteralInt
)
<?>
"integer"
literalFloat
=
tokenParser
(
matchUnary
LiteralFloat
)
<?>
"floating-point number"
literalString
=
tokenParser
(
matchUnary
LiteralString
)
<?>
"string"
literalBool
=
tokenParser
(
matchLiteralBool
)
<?>
"boolean"
literalVoid
=
tokenParser
(
matchSimpleToken
VoidKeyword
)
<?>
"
\"
void
\"
"
atSign
=
tokenParser
(
matchSimpleToken
AtSign
)
<?>
"
\"
@
\"
"
...
...
@@ -121,10 +121,10 @@ interfaceKeyword = tokenParser (matchSimpleToken InterfaceKeyword) <?> "\"interf
optionKeyword
=
tokenParser
(
matchSimpleToken
OptionKeyword
)
<?>
"
\"
option
\"
"
parenthesizedList
parser
=
do
items
<-
tokenParser
matchParenthesizedList
items
<-
tokenParser
(
matchUnary
ParenthesizedList
)
parseList
parser
items
bracketedList
parser
=
do
items
<-
tokenParser
matchBracketedList
items
<-
tokenParser
(
matchUnary
BracketedList
)
parseList
parser
items
declNameBase
::
TokenParser
DeclName
...
...
compiler/src/Token.hs
View file @
6819f272
...
...
@@ -21,12 +21,15 @@
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{-# LANGUAGE DeriveDataTypeable #-}
module
Token
where
import
Data.Generics
import
Text.Parsec.Pos
(
SourcePos
,
sourceLine
,
sourceColumn
)
import
Text.Printf
(
printf
)
data
Located
t
=
Located
{
locatedPos
::
SourcePos
,
locatedValue
::
t
}
data
Located
t
=
Located
{
locatedPos
::
SourcePos
,
locatedValue
::
t
}
deriving
(
Typeable
,
Data
)
instance
Show
t
=>
Show
(
Located
t
)
where
show
(
Located
pos
x
)
=
printf
"%d:%d:%s"
(
sourceLine
pos
)
(
sourceColumn
pos
)
(
show
x
)
...
...
@@ -37,7 +40,7 @@ instance Eq a => Eq (Located a) where
instance
Ord
a
=>
Ord
(
Located
a
)
where
compare
(
Located
_
a
)
(
Located
_
b
)
=
compare
a
b
data
TokenSequence
=
TokenSequence
[
Located
Token
]
SourcePos
deriving
(
Show
,
Eq
)
data
TokenSequence
=
TokenSequence
[
Located
Token
]
SourcePos
deriving
(
Data
,
Typeable
,
Show
,
Eq
)
data
Token
=
Identifier
String
|
TypeIdentifier
String
...
...
@@ -69,7 +72,7 @@ data Token = Identifier String
|
UnionKeyword
|
InterfaceKeyword
|
OptionKeyword
deriving
(
Show
,
Eq
)
deriving
(
Data
,
Typeable
,
Show
,
Eq
)
data
Statement
=
Line
TokenSequence
|
Block
TokenSequence
[
Located
Statement
]
...
...
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