szymski

Untitled

Mar 6th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. > Parsing lua arrays is a nightmare…
  2.  
  3. It is indeed. Whenever you have to process tables consisting of data more advanced just a simple sequence of numbers, you should avoid extracting that data using regular expressions. High sensitivity to data structure changes, different code formatting settings and generally hard readability of regular expressions are only some of the problems it creates... Think you're solving one problem, but in the process you create another one.
  4.  
  5. In most cases of data extraction from another programming language, you should be able to achieve it without reinventing the wheel. What do I mean? In the case of aforementioned Lua, why don't use the Lua parser itself? No language syntax parsing, we just use the tool which has been created for that specific problem. How to get it from Lua to JS? You can use a library which converts Lua tables to JSON. If you don't want to use an external library - just process the table in Lua and print out the data you need to stdout, in an easily parsable format (comma-separated or line by line), might be a JSON as well if you want to. Installing Lua can be achieved by a single command, then invoking it in JS/Node to get stdout is quite easy as well. This way your parsing code will be future-proof, easy to adjust to any changes, protected against different code formattings. You will basically free yourself from having to deal with Lua's syntax and can instead focus on the data itself, which can be then processed in JS.
  6.  
  7. If the input is very simple, with no structure, recursion or so, you will probably save time by doing it all in JavaScript, but well, in most cases, what you're dealing with is cluttered with another language's syntax. If you're trying to create something which is supposed to work long-term, it's better to protect yourself from syntax/formatting changes and keep your code focused on the data itself, not its representation. You'll need less testing.
  8.  
  9.  
Add Comment
Please, Sign In to add comment