Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
BC
public
belr
Commits
47a0ae42
Commit
47a0ae42
authored
Nov 27, 2015
by
Sylvain Berfini
🐮
Browse files
Added possibility to load grammar directly from a string
parent
09d8379a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
13 deletions
+28
-13
include/belr/grammarbuilder.hh
include/belr/grammarbuilder.hh
+12
-1
src/grammarbuilder.cc
src/grammarbuilder.cc
+16
-12
No files found.
include/belr/grammarbuilder.hh
View file @
47a0ae42
...
...
@@ -130,6 +130,17 @@ public:
* Initialize the builder.
**/
ABNFGrammarBuilder
();
/**
* Create a grammar from an ABNF grammar defined in the string pointed by abnf.
* An optional Grammar argument corresponding to a grammar to include can be passed.
* Usually the belr::CoreRules grammar is required for most IETF text protocols.
* The returned grammar can be used to instanciate a belr::Parser object capable of parsing
* the protocol or language described in the grammar.
* @param abnf the string that contains the abnf grammar.
* @param grammar an optional grammar to include.
* @return the Grammar object corresponding to the text definition loaded, NULL if an error occured.
**/
shared_ptr
<
Grammar
>
createFromAbnf
(
const
string
&
abnf
,
const
shared_ptr
<
Grammar
>
&
grammar
=
NULL
);
/**
* Create a grammar from an ABNF grammar defined in the text file pointed by path.
* An optional Grammar argument corresponding to a grammar to include can be passed.
...
...
@@ -140,7 +151,7 @@ public:
* @param grammar an optional grammar to include.
* @return the Grammar object corresponding to the text definition loaded, NULL if an error occured.
**/
shared_ptr
<
Grammar
>
createFromAbnf
(
const
string
&
path
,
const
shared_ptr
<
Grammar
>
&
grammar
=
NULL
);
shared_ptr
<
Grammar
>
createFromAbnf
File
(
const
string
&
path
,
const
shared_ptr
<
Grammar
>
&
grammar
=
NULL
);
private:
Parser
<
shared_ptr
<
ABNFBuilder
>>
mParser
;
};
...
...
src/grammarbuilder.cc
View file @
47a0ae42
...
...
@@ -305,23 +305,16 @@ ABNFGrammarBuilder::ABNFGrammarBuilder()
->
setCollector
(
"dec-val"
,
make_sfn
(
&
ABNFNumval
::
setDecVal
));
}
shared_ptr
<
Grammar
>
ABNFGrammarBuilder
::
createFromAbnf
(
const
string
&
path
,
const
shared_ptr
<
Grammar
>
&
gram
){
shared_ptr
<
Grammar
>
ABNFGrammarBuilder
::
createFromAbnf
(
const
string
&
abnf
,
const
shared_ptr
<
Grammar
>
&
gram
){
size_t
parsed
;
ifstream
istr
(
path
);
if
(
!
istr
.
is_open
()){
cerr
<<
"Could not open "
<<
path
<<
endl
;
return
NULL
;
}
stringstream
sstr
;
sstr
<<
istr
.
rdbuf
();
shared_ptr
<
ABNFBuilder
>
builder
=
mParser
.
parseInput
(
"rulelist"
,
sstr
.
str
(),
&
parsed
);
if
(
parsed
<
(
size_t
)
sstr
.
str
().
size
()){
cerr
<<
"ERROR: only "
<<
parsed
<<
" bytes parsed over a total of "
<<
sstr
.
str
().
size
()
<<
endl
;
shared_ptr
<
ABNFBuilder
>
builder
=
mParser
.
parseInput
(
"rulelist"
,
abnf
,
&
parsed
);
if
(
parsed
<
(
size_t
)
abnf
.
size
()){
cerr
<<
"ERROR: only "
<<
parsed
<<
" bytes parsed over a total of "
<<
abnf
.
size
()
<<
endl
;
return
NULL
;
}
shared_ptr
<
Grammar
>
retGram
;
if
(
gram
==
NULL
)
retGram
=
make_shared
<
Grammar
>
(
path
);
if
(
gram
==
NULL
)
retGram
=
make_shared
<
Grammar
>
(
abnf
);
else
retGram
=
gram
;
builder
->
buildRecognizer
(
retGram
);
cout
<<
"Succesfully created grammar with "
<<
retGram
->
getNumRules
()
<<
" rules."
<<
endl
;
...
...
@@ -335,4 +328,15 @@ shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &path, const
return
gram
;
}
shared_ptr
<
Grammar
>
ABNFGrammarBuilder
::
createFromAbnfFile
(
const
string
&
path
,
const
shared_ptr
<
Grammar
>
&
gram
){
ifstream
istr
(
path
);
if
(
!
istr
.
is_open
()){
cerr
<<
"Could not open "
<<
path
<<
endl
;
return
NULL
;
}
stringstream
sstr
;
sstr
<<
istr
.
rdbuf
();
return
createFromAbnf
(
sstr
.
str
(),
gram
);
}
}
//end of namespace
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