GithubHelp home page GithubHelp logo

Comments (4)

glipari avatar glipari commented on June 12, 2024

I am not sure what you did. Can you just copy your version of css-parser.cpp here ? Otherwise, I cannot replicate your problem.

from tipa.

veksha avatar veksha commented on June 12, 2024

// commands used:
g++ -c tinyparser.cpp property.cpp lexer.cpp -I.
ar rvs tinyparser.a tinyparser.o property.o lexer.o
g++ css_test.cpp tinyparser.a -Itipa/src && a.exe

compiler version:

g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=d:/programm/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/10.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-10.2.0/configure --build=x86_64-w64-mingw32 --enable-targets=all --enable-multilib --enable-64bit --disable-dependency-tracking --prefix=/mingw64 --with-sysroot=/mingw64 --disable-s
hared --enable-static --disable-nls --enable-version-specific-runtime-libs --disable-win32-registry --without-dwarf2 --enable-sjlj-exceptions --enable-fully-dynamic-string --enable-languages=c,d,ada,lto,c+
+,objc,obj-c++,fortran --enable-libgomp --enable-lto --enable-libssp -enable-gnattools --disable-bootstrap --with-gcc --with-gnu-as --with-gnu-ld --with-stabs --enable-interwork --with-mpfr-include=/home/b
etta/gcc-build/../gcc-10.2.0/mpfr/src --with-mpfr-lib=/home/betta/gcc-build/mpfr/src/.libs --enable-objc-gc --with-target-bdw-gc=/mingw64
Thread model: win32
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC)
#include <sstream>
//#include <string>
//#include <stack>
//#include <iostream>
//#include <fstream>
//#include <memory>
//#include <tuple>

#include <tinyparser.hpp>

                                                    /*
// commands used:
g++ -c tinyparser.cpp property.cpp lexer.cpp -I.
ar rvs tinyparser.a tinyparser.o property.o lexer.o
g++ css_test.cpp tinyparser.a -Itipa/src && a.exe
                                                    */

using namespace tipa;


/** example of css :

    button1[device=ipad] {
       font: "Roboto", 12
       textColor: "#FF0000"
       borderColor: "#000000"
    } 

    button1[device=iphone] {
       font: "Roboto", 12
       textColor: "#FF0000"
       borderColor: "#000000"
    } 
*/

int main(int argc, char *argv[])
{

    const token tk_hexacolor = create_lib_token("^#([0-9a-fA-F]{6})");
    
    // First the grammar
    // this is a hexadecimal color
    rule hexaColor = rule("\"") >> rule(tk_hexacolor) >> rule("\"");
    // this is a font
    rule font = rule("font") >> rule(":") >> rule("\"") >> rule(tk_ident) >> rule("\"") >> rule(",") >> rule(tk_int);
    // a textcolor
    rule textColor = rule("textColor") >> rule(":") >> hexaColor;
    // a border color
    rule borderColor = rule("borderColor") >> rule(":") >> hexaColor;    
    // a property is any of font, text, color (they are all optional)
    rule property = font | textColor | borderColor;

    // a button has a name (a identifier), and a device name (another identifier), and a sequence of properties.
    rule button = rule(tk_ident) >> rule('{') >> *property >> rule('}');
    // the whole file is just a list of buttons
    rule root = *button;


    // now the data structures we are going to fill
    using FontS = std::pair<std::string, int>;
    struct ButtonS {
        std::string name;
        std::string device;
        FontS font;
        std::string textColor;
        std::string borderColor;
        
        void clear() { name = ""; device = ""; font.first = ""; font.second = 0; textColor = ""; borderColor = ""; }
    };

    std::vector<ButtonS> buttons;

    ButtonS temp;
    
    // now the parser actions
    // when the parser finds a font, store the values in the temp variable
    font.read_vars(temp.font.first, temp.font.second);
    // same for textColor
    textColor.read_vars(temp.textColor);
    // same for borderColor
    borderColor.read_vars(temp.borderColor);
    
    // when we detect a button, we store the temp variable in the vector of buttons
    button.set_action([&temp, &buttons](parser_context &pc) {
            std::vector<std::string> v;
            pc.collect_tokens(2, back_inserter(v));
            temp.name = v[0];
            temp.device = v[1];
            buttons.push_back(temp);
            temp.clear();
        });
    
    //----------------  end of parser specification -------------

    // An example of css file to parse
    std::stringstream sst(
        "button1 {\n"
        "font: \"Roboto\", 12\n"
        "textColor: \"#FF0000\"\n"
        "borderColor: \"#000000\"\n"
        "}\n" 
        "\n"
        "button1 {\n"
        "font: \"Roboto\", 12\n"
        "textColor: \"#FF0000\"\n"
        "borderColor: \"#000000\"\n"
        "}\n"
        );
    
    parser_context pc;
    pc.set_stream(sst);

    bool f = parse_all(root, pc);
    std::cout << "Parser status : " << std::boolalpha << f << std::endl;
    if (!f) {
        std::cout << pc.get_formatted_err_msg() << std::endl;
    }
    
    for (auto x : buttons) {
        std::cout << "-----------" << std::endl;
        std::cout << "Button : " << x.name << ", " << x.device << std::endl;
        std::cout << "font   : " << x.font.first << ", " << x.font.second << std::endl; 
        std::cout << "text   : " << x.textColor << std::endl;
        std::cout << "border : " << x.borderColor << std::endl;
    }    
}

from tipa.

glipari avatar glipari commented on June 12, 2024

The problem is in the following three lines :

            pc.collect_tokens(2, back_inserter(v));
            temp.name = v[0];
            temp.device = v[1];

In fact, now button has only a name, but not a device, you removed the device name from the rule. Therefore, function collect_tokens tries to collect two elements (the name of the button and the name of the device, but it only gets one, so it thows an exception.

The exception handling was an unfinished part of the library, I was only throwing instances of std::string. To avoid such confusion, I just replaced all the throw std::string with a throw parse_exc, which is just a subclass of std::exception. Therefore, for debugging purposes, you can try the following

    bool f = false;
    try {
        f = parse_all(root, pc);
    } catch(parse_exc exc) {
        std::cout << "exception caught" << std::endl;
        std::cout << exc.what() << std::endl;
        f = false;
    }

Which should print a slightly better message.
Regarding your error, you should replace the previous buggy code with

            pc.collect_tokens(1, back_inserter(v));
            temp.name = v[0];

Best regards

from tipa.

veksha avatar veksha commented on June 12, 2024

big thanks!

from tipa.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.