Parsing a C++ function declaration with Perl 6 grammar











up vote
0
down vote

favorite












I wrote a Perl 6 grammar to parse a C++ function. My final goal would be to parse an entire header. The aim is not to correct C++ syntax errors, but to parse valid C++.



Do you have some advice or improvements?



#!/usr/bin/env perl6

grammar FUNCTION {
token TOP { [ <attr> s+ ]? <type> [ s* <type_mod> ]?
(s+) <fname> (s*) "("
(s*) [<parameter> [ "," (s*) <parameter> ]* ]? (s*)
')'(s*) ';'
}

token name { w+ }
token namespace { [ "::" ]? [ <name> "::" ]* }
token attr { <name> }
token type { <namespace>? <name> }
token type_mod { [ *|& ]+ }
token fname { <name> }
token variable { <name> }
token parameter { <type> [s* <type_mod> ]? s+ <variable> }
}

my $str = "const ::one::std::string ** ma1n( int&& i, two::std::string va1e_ );";
my $parsed = FUNCTION.parse($str);
say $parsed;









share|improve this question




























    up vote
    0
    down vote

    favorite












    I wrote a Perl 6 grammar to parse a C++ function. My final goal would be to parse an entire header. The aim is not to correct C++ syntax errors, but to parse valid C++.



    Do you have some advice or improvements?



    #!/usr/bin/env perl6

    grammar FUNCTION {
    token TOP { [ <attr> s+ ]? <type> [ s* <type_mod> ]?
    (s+) <fname> (s*) "("
    (s*) [<parameter> [ "," (s*) <parameter> ]* ]? (s*)
    ')'(s*) ';'
    }

    token name { w+ }
    token namespace { [ "::" ]? [ <name> "::" ]* }
    token attr { <name> }
    token type { <namespace>? <name> }
    token type_mod { [ *|& ]+ }
    token fname { <name> }
    token variable { <name> }
    token parameter { <type> [s* <type_mod> ]? s+ <variable> }
    }

    my $str = "const ::one::std::string ** ma1n( int&& i, two::std::string va1e_ );";
    my $parsed = FUNCTION.parse($str);
    say $parsed;









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I wrote a Perl 6 grammar to parse a C++ function. My final goal would be to parse an entire header. The aim is not to correct C++ syntax errors, but to parse valid C++.



      Do you have some advice or improvements?



      #!/usr/bin/env perl6

      grammar FUNCTION {
      token TOP { [ <attr> s+ ]? <type> [ s* <type_mod> ]?
      (s+) <fname> (s*) "("
      (s*) [<parameter> [ "," (s*) <parameter> ]* ]? (s*)
      ')'(s*) ';'
      }

      token name { w+ }
      token namespace { [ "::" ]? [ <name> "::" ]* }
      token attr { <name> }
      token type { <namespace>? <name> }
      token type_mod { [ *|& ]+ }
      token fname { <name> }
      token variable { <name> }
      token parameter { <type> [s* <type_mod> ]? s+ <variable> }
      }

      my $str = "const ::one::std::string ** ma1n( int&& i, two::std::string va1e_ );";
      my $parsed = FUNCTION.parse($str);
      say $parsed;









      share|improve this question















      I wrote a Perl 6 grammar to parse a C++ function. My final goal would be to parse an entire header. The aim is not to correct C++ syntax errors, but to parse valid C++.



      Do you have some advice or improvements?



      #!/usr/bin/env perl6

      grammar FUNCTION {
      token TOP { [ <attr> s+ ]? <type> [ s* <type_mod> ]?
      (s+) <fname> (s*) "("
      (s*) [<parameter> [ "," (s*) <parameter> ]* ]? (s*)
      ')'(s*) ';'
      }

      token name { w+ }
      token namespace { [ "::" ]? [ <name> "::" ]* }
      token attr { <name> }
      token type { <namespace>? <name> }
      token type_mod { [ *|& ]+ }
      token fname { <name> }
      token variable { <name> }
      token parameter { <type> [s* <type_mod> ]? s+ <variable> }
      }

      my $str = "const ::one::std::string ** ma1n( int&& i, two::std::string va1e_ );";
      my $parsed = FUNCTION.parse($str);
      say $parsed;






      c++ parsing grammar perl6 lexer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 31 mins ago









      Jamal

      30.2k11115226




      30.2k11115226










      asked 8 hours ago









      tejas

      20116




      20116






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          I'd expect to see many more tests of any program that addresses a problem this gnarly.



          Choosing a couple I've recently had cause to write (on Stack Overflow), I would immediately add







          • void (SENDER::*get_func())(double, double);


            (from Passing pointers to member function as returned values to QObject::connect())


          • constexpr std::size_t len(const T(&)[length]);


            (from Differentiate between Array and Pointer in function parameter)


          Neither of these succeeded when I tried them.






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208179%2fparsing-a-c-function-declaration-with-perl-6-grammar%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            I'd expect to see many more tests of any program that addresses a problem this gnarly.



            Choosing a couple I've recently had cause to write (on Stack Overflow), I would immediately add







            • void (SENDER::*get_func())(double, double);


              (from Passing pointers to member function as returned values to QObject::connect())


            • constexpr std::size_t len(const T(&)[length]);


              (from Differentiate between Array and Pointer in function parameter)


            Neither of these succeeded when I tried them.






            share|improve this answer

























              up vote
              1
              down vote













              I'd expect to see many more tests of any program that addresses a problem this gnarly.



              Choosing a couple I've recently had cause to write (on Stack Overflow), I would immediately add







              • void (SENDER::*get_func())(double, double);


                (from Passing pointers to member function as returned values to QObject::connect())


              • constexpr std::size_t len(const T(&)[length]);


                (from Differentiate between Array and Pointer in function parameter)


              Neither of these succeeded when I tried them.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                I'd expect to see many more tests of any program that addresses a problem this gnarly.



                Choosing a couple I've recently had cause to write (on Stack Overflow), I would immediately add







                • void (SENDER::*get_func())(double, double);


                  (from Passing pointers to member function as returned values to QObject::connect())


                • constexpr std::size_t len(const T(&)[length]);


                  (from Differentiate between Array and Pointer in function parameter)


                Neither of these succeeded when I tried them.






                share|improve this answer












                I'd expect to see many more tests of any program that addresses a problem this gnarly.



                Choosing a couple I've recently had cause to write (on Stack Overflow), I would immediately add







                • void (SENDER::*get_func())(double, double);


                  (from Passing pointers to member function as returned values to QObject::connect())


                • constexpr std::size_t len(const T(&)[length]);


                  (from Differentiate between Array and Pointer in function parameter)


                Neither of these succeeded when I tried them.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 4 hours ago









                Toby Speight

                22.2k536108




                22.2k536108






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208179%2fparsing-a-c-function-declaration-with-perl-6-grammar%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'