DEV Community

Discussion on: Brace matching, it's harder than it sounds!

Collapse
rgaiken profile image
rgaiken

Try using a stack for all bracket elements. So everything like ({[ and whatever else I'm forgetting. Store the bracket type along with the line number and any other useful debugging. If you get a closing bracket, check it against the top of the stack. If it completes the top of stack bracket, pop it. If it doesn't match, throw an error, using the debugging info from both the current line and the top of the stack. At end of file, the stack should be empty.

Collapse
adam_cyclones profile image
Adam Crockett Author

Now this is the traditional approach, I admit I dismissed this as I need to scrape the content between braces and know the depth, as this is a scss + js like language, I will take a look at using a stake and additional scraping content. Thanks for the suggestion 😁