Analisi delle stringhe sfuggite con boost spirit
-
26-09-2019 - |
Domanda
Sto lavorando con Spirit 2.4 e vorrei analizzare una struttura come questa:
Testo{campo_testo};
Il punto è che in text_field c'è una stringa di escape con i simboli '{', '}' e '\'.Vorrei creare un parser per questo usando qi.Ho provato questo:
using boost::spirit::standard::char_;
using boost::spirit::standard::string;
using qi::lexeme;
using qi::lit;
qi::rule< IteratorT, std::string(), ascii::space_type > text;
qi::rule< IteratorT, std::string(), ascii::space_type > content;
qi::rule< IteratorT, std::string(), ascii::space_type > escChar;
text %=
lit( "Text" ) >> '{' >>
content >>
"};"
;
content %= lexeme[ +( +(char_ - ( lit( '\\' ) | '}' ) ) >> escChar ) ];
escChar %= string( "\\\\" )
| string( "\\{" )
| string( "\\}" );
Ma non viene nemmeno compilato.Qualche idea?
Soluzione
La tua grammatica potrebbe essere scritta come:
qi::rule< IteratorT, std::string(), ascii::space_type > text;
qi::rule< IteratorT, std::string() > content;
qi::rule< IteratorT, char() > escChar;
text = "Text{" >> content >> "};";
content = +(~char_('}') | escChar);
escChar = '\\' >> char_("\\{}");
cioè.
testo È
Text{
seguito da contenuto seguito da}
contenuto è almeno un'istanza di entrambi un personaggio (ma no
}
) o un ESCCHARescChar è un singolo sfuggito
\\
,{
, O}
Notare la escChar la regola ora restituisce un singolo carattere e scarta l'escape \\
.Non sono sicuro che sia quello di cui hai bisogno.Inoltre, ho rimosso lo skipper per il file contenuto E escChar regole, che consente di tralasciare il lexeme[]
(una regola senza skipper si comporta come un lessema implicito).