Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hammer
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Denley Lam
hammer
Commits
a5c579c2
Commit
a5c579c2
authored
12 years ago
by
Sven M. Hallberg
Browse files
Options
Downloads
Patches
Plain Diff
switch semantic base64 examples to pre-fab actions provided by glue.h
parent
38ddcc5a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/base64_sem1.c
+8
-56
8 additions, 56 deletions
examples/base64_sem1.c
examples/base64_sem2.c
+4
-41
4 additions, 41 deletions
examples/base64_sem2.c
with
12 additions
and
97 deletions
examples/base64_sem1.c
+
8
−
56
View file @
a5c579c2
...
@@ -13,50 +13,11 @@
...
@@ -13,50 +13,11 @@
// base64_sem2.c for an alternative approach using a single top-level action.
// base64_sem2.c for an alternative approach using a single top-level action.
#include
"../src/hammer.h"
#include
"../src/hammer.h"
#include
"../src/glue.h"
#include
"../src/internal.h"
// for h_carray functions (XXX ?!)
#include
"../src/internal.h"
// for h_carray functions (XXX ?!)
#include
<assert.h>
#include
<assert.h>
#define H_RULE(rule, def) const HParser *rule = def
#define H_ARULE(rule, def) const HParser *rule = h_action(def, act_ ## rule)
///
// Semantic action helpers.
// These might be candidates for inclusion in the library.
///
// The action equivalent of h_ignore.
const
HParsedToken
*
act_ignore
(
const
HParseResult
*
p
)
{
return
NULL
;
}
// Helper to build HAction's that pick one index out of a sequence.
const
HParsedToken
*
act_index
(
int
i
,
const
HParseResult
*
p
)
{
if
(
!
p
)
return
NULL
;
const
HParsedToken
*
tok
=
p
->
ast
;
if
(
!
tok
||
tok
->
token_type
!=
TT_SEQUENCE
)
return
NULL
;
const
HCountedArray
*
seq
=
tok
->
seq
;
size_t
n
=
seq
->
used
;
if
(
i
<
0
||
(
size_t
)
i
>=
n
)
return
NULL
;
else
return
tok
->
seq
->
elements
[
i
];
}
const
HParsedToken
*
act_index0
(
const
HParseResult
*
p
)
{
return
act_index
(
0
,
p
);
}
///
///
// Semantic actions for the grammar below, each corresponds to an "ARULE".
// Semantic actions for the grammar below, each corresponds to an "ARULE".
// They must be named act_<rulename>.
// They must be named act_<rulename>.
...
@@ -84,11 +45,13 @@ const HParsedToken *act_bsfdig(const HParseResult *p)
...
@@ -84,11 +45,13 @@ const HParsedToken *act_bsfdig(const HParseResult *p)
return
res
;
return
res
;
}
}
H_ACT_APPLY
(
act_index0
,
h_act_index
,
0
);
#define act_bsfdig_4bit act_bsfdig
#define act_bsfdig_4bit act_bsfdig
#define act_bsfdig_2bit act_bsfdig
#define act_bsfdig_2bit act_bsfdig
#define act_equals act_ignore
#define act_equals
h_
act_ignore
#define act_ws act_ignore
#define act_ws
h_
act_ignore
#define act_document act_index0
#define act_document act_index0
...
@@ -124,20 +87,9 @@ const HParsedToken *act_base64_n(int n, const HParseResult *p)
...
@@ -124,20 +87,9 @@ const HParsedToken *act_base64_n(int n, const HParseResult *p)
return
res
;
return
res
;
}
}
const
HParsedToken
*
act_base64_3
(
const
HParseResult
*
p
)
H_ACT_APPLY
(
act_base64_3
,
act_base64_n
,
3
);
{
H_ACT_APPLY
(
act_base64_2
,
act_base64_n
,
2
);
return
act_base64_n
(
3
,
p
);
H_ACT_APPLY
(
act_base64_1
,
act_base64_n
,
1
);
}
const
HParsedToken
*
act_base64_2
(
const
HParseResult
*
p
)
{
return
act_base64_n
(
2
,
p
);
}
const
HParsedToken
*
act_base64_1
(
const
HParseResult
*
p
)
{
return
act_base64_n
(
1
,
p
);
}
// Helper to concatenate two arrays.
// Helper to concatenate two arrays.
void
carray_concat
(
HCountedArray
*
a
,
const
HCountedArray
*
b
)
void
carray_concat
(
HCountedArray
*
a
,
const
HCountedArray
*
b
)
...
...
This diff is collapsed.
Click to expand it.
examples/base64_sem2.c
+
4
−
41
View file @
a5c579c2
...
@@ -14,50 +14,11 @@
...
@@ -14,50 +14,11 @@
// transformation.
// transformation.
#include
"../src/hammer.h"
#include
"../src/hammer.h"
#include
"../src/glue.h"
#include
"../src/internal.h"
// for h_carray functions (XXX ?!)
#include
"../src/internal.h"
// for h_carray functions (XXX ?!)
#include
<assert.h>
#include
<assert.h>
#define H_RULE(rule, def) const HParser *rule = def
#define H_ARULE(rule, def) const HParser *rule = h_action(def, act_ ## rule)
///
// Semantic action helpers.
// These might be candidates for inclusion in the library.
///
// The action equivalent of h_ignore.
const
HParsedToken
*
act_ignore
(
const
HParseResult
*
p
)
{
return
NULL
;
}
// Helper to build HAction's that pick one index out of a sequence.
const
HParsedToken
*
act_index
(
int
i
,
const
HParseResult
*
p
)
{
if
(
!
p
)
return
NULL
;
const
HParsedToken
*
tok
=
p
->
ast
;
if
(
!
tok
||
tok
->
token_type
!=
TT_SEQUENCE
)
return
NULL
;
const
HCountedArray
*
seq
=
tok
->
seq
;
size_t
n
=
seq
->
used
;
if
(
i
<
0
||
(
size_t
)
i
>=
n
)
return
NULL
;
else
return
tok
->
seq
->
elements
[
i
];
}
const
HParsedToken
*
act_index0
(
const
HParseResult
*
p
)
{
return
act_index
(
0
,
p
);
}
///
///
// Semantic actions for the grammar below, each corresponds to an "ARULE".
// Semantic actions for the grammar below, each corresponds to an "ARULE".
// They must be named act_<rulename>.
// They must be named act_<rulename>.
...
@@ -150,7 +111,9 @@ const HParsedToken *act_base64(const HParseResult *p)
...
@@ -150,7 +111,9 @@ const HParsedToken *act_base64(const HParseResult *p)
return
res
;
return
res
;
}
}
#define act_ws act_ignore
H_ACT_APPLY
(
act_index0
,
h_act_index
,
0
);
#define act_ws h_act_ignore
#define act_document act_index0
#define act_document act_index0
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment