libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
mya.h File Reference
#include "module.h"
#include "token.h"
Include dependency graph for mya.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

error_code_t mya_lexer (module_t *module)
 Make the lexical analysis on the given module.
 
error_code_t mya_parser (module_t *module)
 Make the syntactical analysis on the given module and construct the AST.
 

Function Documentation

◆ mya_lexer()

error_code_t mya_lexer ( module_t * module)

Make the lexical analysis on the given module.

It will construct the module->tokens list and maybe registry some errors on module->errors list.

Parameters
moduleThe module for make the lexical analysis.

Definition at line 53 of file lexer.c.

54{
55 char message[128];
56 int ch;
57 unsigned int line = 1;
58 unsigned int column = 1;
60
61 for (; module_lookup(module, &ch, 0) == ERR_OK; column++) {
62 if (isblank(ch)) {
63 module_getc(module, &ch);
64 continue;
65 }
66
67 switch (ch) {
68 case '#':
69 _mod_rm_line(module);
70 line++;
71 column = 0;
72 continue;
73 case '\n':
74 line++;
75 column = 0;
76 break;
77 case '{':
79 break;
80 case '}':
82 break;
83 case '(':
85 break;
86 case ')':
88 break;
89 case '[':
91 break;
92 case ']':
94 break;
95 case ';':
97 break;
98 case ':':
99 MOD_ADD(":", TK_COLON);
100 break;
101 case ',':
102 MOD_ADD(",", TK_COMMA);
103 break;
104 case '=':
105 MOD_ADD("=", TK_EQUAL);
106 break;
107 case '"':
108 column += _mod_read_string(module, line, column) - 1;
109 continue;
110 default:
111 if (isdigit(ch)) {
112 column += _mod_read_number(module, line, column) - 1;
113 continue;
114 }
115
116 if (ispunct(ch)) {
117 column += _mod_read_operator(module, line, column) - 1;
118 continue;
119 }
120
121 if (isalnum(ch)) {
122 column += _mod_read_identifier(module, line, column) - 1;
123 continue;
124 }
125
126 sprintf(message, "Character '%c' is unexpected here!\n", ch);
127
128 module_add_error(module, line, column, 1, message);
129 break;
130 }
131
132 module_getc(module, &ch);
133 }
134
135 MOD_ADD(":EOF:", TK_EOF);
136
137 return ERR_OK;
138}
@ ERR_OK
Definition err.h:15
#define MOD_ADD(lexeme, type)
Definition lexer.c:47
void module_add_error(module_t *module, unsigned int line, unsigned int column, unsigned int length, const char *message)
Add error for the given module.
Definition module.c:92
error_code_t module_lookup(module_t *module, int *chret, unsigned int seek)
Get a character on module's file, without removing it from the queue.
Definition module.c:63
error_code_t module_getc(module_t *module, int *chret)
Get next character on module's file, removing it from the queue.
Definition module.c:55
Struct that represents a Mya module.
Definition module.h:34
Struct for a Mya token.
Definition token.h:32
struct token token_t
Struct for a Mya token.
@ TK_OPEN_BRACKET
Definition token.h:21
@ TK_COLON
Definition token.h:13
@ TK_OPEN_BRACES
Definition token.h:20
@ TK_OPEN_PARENS
Definition token.h:22
@ TK_COMMA
Definition token.h:14
@ TK_CLOSE_PARENS
Definition token.h:12
@ TK_CLOSE_BRACKET
Definition token.h:11
@ TK_EOF
Definition token.h:15
@ TK_CLOSE_BRACES
Definition token.h:10
@ TK_EQUAL
Definition token.h:16
@ TK_SEMICOLON
Definition token.h:24

◆ mya_parser()

error_code_t mya_parser ( module_t * module)

Make the syntactical analysis on the given module and construct the AST.

Parameters
moduleThe module for make the syntactical analysis.

Definition at line 9 of file parser.c.

10{
12
13 for (unsigned int tk_index = 0; tk_index < module->tokens_count; tk_index++) {
14 token = &module->tokens[tk_index];
15
16 switch (token->type) {
17 case TK_KEYWORD:
18 tk_index += parse_statement(module, &module->ast, token) - 1;
19 break;
20 default:
21 return ERR_EMPTY;
22 }
23 }
24
25 return ERR_OK;
26}
@ ERR_EMPTY
Definition err.h:16
unsigned int parse_statement(module_t *module, ast_node_t *parent, token_t *token)
Parse a statement adding it as a children on parent AST node.
ast_node_t ast
AST of the module.
Definition module.h:37
unsigned int tokens_count
Number of tokens on tokens list.
Definition module.h:39
token_type_t type
Token type.
Definition token.h:33
@ TK_KEYWORD
Definition token.h:18