0

I have a file stored and I'm reading it with Javascript. The problem is that I want to simulate a syntactic validator and I can't get a good result.

syntax.txt:

class foo {
}

The problem is that I can't get the class content. What I do is verify if there's a class statement:

fileString.match(\^class\g);

This returns me: ["class"], but I want to get foo { } and everething inside.

This is possible?

And what happend if the file changes? something like: syntax.txt:

class foo {
}
class bar {
}

Thanks!

Tomás Juárez
  • 1,517
  • 3
  • 21
  • 51
  • 2
    well your reg exp only looks for the class portion...have fun with the opening and closing } inside of it. It will most likely take more than a reg exp to get the string. – epascarello Nov 18 '13 at 18:59
  • I'm trying to do it by drawing a directed graph for a finite automaton! – Tomás Juárez Nov 18 '13 at 19:01

1 Answers1

2

You aren't going to be able to validate a programming language with regex, for the same reason you can't validate HTML with regex. It's not regular.

Imagine this (pseudo code):

class foo {
    function barbar() {
        if(foobar) {
            case(bar) {
                'x': ... break;
                'y':
                    if(foofoo) {
                        ...
                        return "}";
                    }
            }
        }
    }
}

You'll never be able to handle all of the scenarios in regex. You need a true parser. You can write your own or use a library, but you definitely cannot rely on regex for this.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Yes, it's true, but I only want to get the text inside the class – Tomás Juárez Nov 18 '13 at 19:10
  • 2
    You can't do it. You can't just search until you hit `}`. And you can't force a JavaScript regex to count instances of `{` and `}`, or process quotes, or analyze cases when the syntax is just wrong, like a missing `}`. Regex won't cut it. – elixenide Nov 18 '13 at 19:13
  • And whats if I say: the class is defined by ´class foo [ ]´ and everithing else uses ´{}´? – Tomás Juárez Nov 18 '13 at 19:25
  • How would that help? There is nothing out there to keep me from doing something like this inside a JavaScript class: `var x = '{'+"{}{{}"+(function(){ return {x: true};})().x+(y.replace(/[{}]+/,"bar"));`. This is a perfectly valid (if incoherent) assignment. Good luck parsing all of the perfectly legitimate uses of `{`,`}`,`[`,`]`,`(`,`)`,`"`,`'`,`/`, etc. – elixenide Nov 18 '13 at 19:39