cpp-peglib/example/docx.cc

65 lines
176 KiB
C++
Raw Normal View History

2023-03-25 12:42:56 +00:00
//
// docx.cc
//
// Copyright (c) 2022 Yuji Hirose. All rights reserved.
// MIT License
//
#include <cstdlib>
#include <iostream>
#include <peglib.h>
using namespace peg;
int main(void) {
parser parser(R"(ROOT <- DECL CONTENT
DECL <- '<?' (!('?>') .)+ '?>'
CONTENT <- (ELEMENT / TEXT)*
ELEMENT <- $(STAG CONTENT ETAG) / ATAG
STAG <- '<' $tag< TAG_NAME > (' ' ATTRIBUTES)?'>'
ETAG <- '</' $tag '>'
ATAG <- '<' TAG_NAME (' ' ATTRIBUTES)? '/>'
TAG_NAME <- < [a-zA-Z:]+ >
ATTRIBUTES <- (!('>' / '/>') .)+
TEXT <- < TEXT_DATA+ >
TEXT_DATA <- ![<] .
)");
std::vector<std::string> stack;
stack.push_back("[ROOT]");
parser["STAG"] = [&](const SemanticValues &vs) {
auto tag = std::any_cast<std::string>(vs[0]);
if (tag == "w:pPr" || tag == "w:t") {
std::cout << "<" << tag << ">" << std::endl;
stack.push_back(tag);
}
};
parser["ETAG"] = [&](const SemanticValues & /*vs*/) {
auto tag = stack.back();
if (tag == "w:pPr" || tag == "w:t") {
std::cout << "</" << tag << ">" << std::endl;
stack.pop_back();
}
};
parser["TAG_NAME"] = [&](const SemanticValues &vs) {
return vs.token_to_string();
};
parser["TEXT"] = [&](const SemanticValues &vs) {
auto tag = stack.back();
if (tag == "w:t") { std::cout << vs.sv(); }
};
const auto source = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="00425316" w:rsidRPr="00360EA4" w:rsidRDefault="00425316" w:rsidP="00425316"><w:pPr><w:spacing w:line="240" w:lineRule="auto"/><w:rPr><w:rFonts w:ascii="MEPS Bookman Universal" w:eastAsia="MS Mincho" w:hAnsi="MEPS Bookman Universal" w:cs="MEPS Bookman Universal"/><w:b/><w:bCs/><w:sz w:val="22"/><w:szCs w:val="22"/><w:lang w:eastAsia="ja-JP"/></w:rPr></w:pPr><w:r w:rsidRPr="00360EA4"><w:rPr><w:rFonts w:ascii="MEPS Bookman Universal" w:eastAsia="MS Mincho" w:hAnsi="MEPS Bookman Universal" w:cs="MEPS Bookman Universal"/><w:sz w:val="22"/><w:szCs w:val="22"/><w:lang w:eastAsia="ja-JP"/></w:rPr><w:t></w:t></w:r><w:r w:rsidRPr="00360EA4"><w:rPr><w:rFonts w:ascii="MEPS Bookman Universal" w:eastAsia="MS Mincho" w:hAnsi="MEPS Bookman Universal" w:cs="MEPS Bookman Universal"/><w:b/><w:sz w:val="22"/><w:szCs w:val="22"/><w:lang w:eastAsia="ja-JP"/></w:rPr><w:t></w:t></w:r></w:p><w:p w:rsidR="00425316" w:rsidRPr="00360EA4" w:rsidRDefault="00425316" w:rsidP="00425316"><w:pPr><w:spacing w:line="240" w:lineRule="auto"/><w:rPr><w:rFonts w:ascii="MEPS Bookman Universal" w:eastAsia="MS Mincho" w:hAnsi="MEPS Bookman Universal" w:cs="MEPS Bookman Universal"/><w:sz w:val="22"/><w:szCs w:val="22"/><w:lang w:eastAsia="ja-JP"/></w:rPr></w:pPr><w:r w:rsidRPr="00360EA4"><w:rPr><w:rFonts w:ascii="MEPS Bookman Universal" w:eastAsia="MS Mincho" w:hAnsi="MEPS Bookman Universal" w:cs="MEPS Bookman Universal"/><w:sz w:val="22"/><w:szCs w:val="22"/><w:lang w:eastAsia="ja-JP"/></w:rPr><w:t></w:t></w:r><w:r w:rsidRPr="00360EA4"><w:rPr><w:rFonts w:ascii="MEPS Bookman Universal" w:eastAsia="MS Mincho" w:hAnsi="MEPS Bookman Universal" w:cs="MEPS Bookman Universal"/><w:color w:val="050505"/><w:sz w:val="22"/><w:szCs w:val="22"/><w:lang w:eastAsia="ja-JP"/></w:rPr><w:t></w:t></w:r></w:p><w:p w:rsidR="00425316" w:rsidRPr="00360EA4" w:rsidRDefault="00425316" w:rsidP="00425316"><w:pPr><w:spacing w:line="240" w:lineRule="auto"/><w:rPr><w:rFonts w:ascii="MEPS Book
)";
if (parser.parse(source)) { return 0; }
std::cout << "syntax error..." << std::endl;
return -1;
}