pashmak

Lexer and Parser API

Lexer

Lexer is in src/core/lexer.py.

parse_op: Parses one operation

Args:

returns a dict. Strructure:

{
    "command": "<the command>",
    "args_str": "arguments of command as string",
    "str": "all of command as string",
    "args": ["arguments", "as", "list"], // seprated by ` ` space
    "file_path": "/path/to/file/that/this/line/loaded/from",
    "strings": ["<output of `parse_string`>"],
    "line_number": 12
}

Example:

print(lexer.parse_op("println('hello world')", "/path/to/file/that/this/line/loaded/from", 12))
# OR: `println 'hello world'`

output:

{
    "command": "println",
    "args_str": "('hello world')",
    "str": "println('hello world')",
    "args": ["('hello", "world')"], // seprated by ` ` space
    "file_path": "/path/to/file/that/this/line/loaded/from",
    "strings": ["<output of `parse_string`>"],
    "line_number": 12
}

parse_string: Splits strings and codes

Args:

Returns a list from other lists:

[
    [false, "println("],
    [true, "'hello world'"],
    [false, ")"],
]

(the above output is for println('hello world')). The first boolean item, if is True, means that this part is a string, But if is False, means this is a native code. And the second item is the code as string.

This function is useful when you want to replace/etc something on a code, But only in native code and not on strings.

parse_eval: Converts the Pashmak eval code Python code

Args:

Returns a string from generated python code.

Example:

output = lexer.parse_eval('$name + "."')

py_code = ''

for item in output:
    py_code += item[-1]

eval(py_code)

multi_char_split: Splits by more than 1 character

Example:

print(lexer.multi_char_split('12+19*14', '+*'))

output:

['12', '19', '14']

Also you can give the count:

print(lexer.multi_char_split('12+19*14', '+*'), 1)

output:

['12', '19*14']

Parser

Parser is in src/core/parser.py.

parse: Parses a code

This is The main parser function.

Args:

Returns a list:

[
    {<output of lexer.parse_op>},
    {<output of lexer.parse_op>},
    {<output of lexer.parse_op>},
    ...
]

Handles multiline and if statements.

split_by_equals: Splits <something> = <something> syntax

Args:

Returns a list.

Example for $name = 'pashmak': [‘$name’, “‘pashmak’”]

Example for println($name): [‘println($name)’]

If output is a list with 1 item, means this is a not <a> = <b>. But if yes, first item is <a>(before =) and second it after =.

Other example:

print(parser.split_by_equals('$age = 30'))

output:

["$age", "30"]

Also:

print(parser.split_by_equals('somefunc()'))

output:

["somefunc()"]