summaryrefslogtreecommitdiffhomepage
path: root/tools/mirb/mirb.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/mirb/mirb.c')
-rw-r--r--tools/mirb/mirb.c79
1 files changed, 29 insertions, 50 deletions
diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c
index f09417ed0..a5285df0f 100644
--- a/tools/mirb/mirb.c
+++ b/tools/mirb/mirb.c
@@ -20,6 +20,31 @@ is_code_block_open(struct mrb_parser_state *parser)
{
int code_block_open = FALSE;
+ /* check for unterminated string */
+ if (parser->sterm) return TRUE;
+
+ /* check if parser error are available */
+ if (0 < parser->nerr) {
+ const char *unexpected_end = "syntax error, unexpected $end";
+ const char *message = parser->error_buffer[0].message;
+
+ /* a parser error occur, we have to check if */
+ /* we need to read one more line or if there is */
+ /* a different issue which we have to show to */
+ /* the user */
+
+ if (strncmp(message, unexpected_end, strlen(unexpected_end)) == 0) {
+ code_block_open = TRUE;
+ }
+ else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) {
+ code_block_open = TRUE;
+ }
+ else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) {
+ code_block_open = TRUE;
+ }
+ return code_block_open;
+ }
+
switch (parser->lstate) {
/* all states which need more code */
@@ -27,7 +52,6 @@ is_code_block_open(struct mrb_parser_state *parser)
case EXPR_BEG:
/* an expression was just started, */
/* we can't end it like this */
- if (parser->nerr == 0) return FALSE;
code_block_open = TRUE;
break;
case EXPR_DOT:
@@ -80,52 +104,6 @@ is_code_block_open(struct mrb_parser_state *parser)
break;
}
- if (!code_block_open) {
- /* based on the last parser state the code */
- /* block seems to be closed */
-
- /* now check if parser error are available */
- if (0 < parser->nerr) {
- /* a parser error occur, we have to check if */
- /* we need to read one more line or if there is */
- /* a different issue which we have to show to */
- /* the user */
-
- if (strcmp(parser->error_buffer[0].message,
- "syntax error, unexpected $end, expecting ';' or '\\n'") == 0) {
- code_block_open = TRUE;
- }
- else if (strcmp(parser->error_buffer[0].message,
- "syntax error, unexpected $end, expecting keyword_end") == 0) {
- code_block_open = TRUE;
- }
- else if (strcmp(parser->error_buffer[0].message,
- "syntax error, unexpected $end, expecting '<' or ';' or '\\n'") == 0) {
- code_block_open = TRUE;
- }
- else if (strcmp(parser->error_buffer[0].message,
- "syntax error, unexpected keyword_end") == 0) {
- code_block_open = TRUE;
- }
- else if (strcmp(parser->error_buffer[0].message,
- "syntax error, unexpected $end, expecting keyword_then or ';' or '\\n'") == 0) {
- code_block_open = TRUE;
- }
- else if (strcmp(parser->error_buffer[0].message,
- "syntax error, unexpected tREGEXP_BEG") == 0) {
- code_block_open = TRUE;
- }
- else if (strcmp(parser->error_buffer[0].message,
- "unterminated string meets end of file") == 0) {
- code_block_open = TRUE;
- }
- }
- }
- else {
- /* last parser state suggest that this code */
- /* block is open, WE NEED MORE CODE!! */
- }
-
return code_block_open;
}
@@ -184,7 +162,7 @@ main(void)
last_code_line[char_index] = '\0';
- if (strcmp(last_code_line, "exit") == 0) {
+ if (strcmp(last_code_line, "quit") == 0) {
if (code_block_open) {
/* cancel the current block and reset */
code_block_open = FALSE;
@@ -199,18 +177,19 @@ main(void)
}
else {
if (code_block_open) {
+ strcat(ruby_code, "\n");
strcat(ruby_code, last_code_line);
}
else {
memset(ruby_code, 0, sizeof(*ruby_code));
strcat(ruby_code, last_code_line);
}
- strcat(ruby_code, "\n");
/* parse code */
parser->s = ruby_code;
parser->send = ruby_code + strlen(ruby_code);
parser->capture_errors = 1;
+ parser->lineno = 1;
mrb_parser_parse(parser);
code_block_open = is_code_block_open(parser);
@@ -220,7 +199,7 @@ main(void)
else {
if (0 < parser->nerr) {
/* syntax error */
- printf("%s\n", parser->error_buffer[0].message);
+ printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message);
}
else {
/* generate bytecode */