D
D
dimkss2017-07-18 12:30:15
Java
dimkss, 2017-07-18 12:30:15

How to solve the parsing of code lines like "public function integer", "event type string f_function"?

Advise how to parse the code, preferably simpler. The language is uninteresting, similar to Basic a bit.
Lots of declarations like:

protected function integer f_todo_something (long al_row)
event e_go_somewhere()
constant string DW_REPORT = "d_not_so_cool_report"

Ideally, for a string like
protected function integer f_todo_something (long al_row)

get the result of the struct type (hashMap, object, doesn't matter):
type = "Function"
scope = "Protected"
returnType = "Integer"
name = "f_todo_something"
arg1 = "long", "al_row"

As I understand it, you need to dig in the direction of AST , maybe there is something simple?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2017-07-18
@dimkss

What we can summarize:
1. There is order. The elements in the sequence are not required, but the order is strict
. For example, protected may not be. But it definitely cannot be function protected
2. Part of the elements has only a limited set of values ​​(scope, type)
I see, approximately, the following logic (it needs to be drawn like a diagram):
1. define a set of valid values ​​for scope (protected/public/ private)
2. take the first word of the string. we look if it matches one of the options
3. if not, then set the scope by default (public for example). And we check the first word for coincidence with the set of valid values ​​of type (function/class/enum/interface).
4. if not, a parsing error
5. if there were no errors and the type value has not yet been defined, then scope was defined before . Therefore, we take the second word and check it for a set of valid values ​​type
6. if type has already been defined, we check for a set for returnType
7. when we define all the first three elements (type, scope, returnType), we take the 4th - this is the name of the function
8. we take the value in (brackets) and divide by the delimeter "," - we get an array of arguments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question