summaryrefslogtreecommitdiff
path: root/parse.y
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-11-04 21:23:11 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-11-04 23:17:34 +0900
commit26316cc350109ba71d42f944f3b976985627c042 (patch)
tree695c56cff39ce10ef4e30e59cce535d076cac650 /parse.y
parentc303854e134043d905baff2385add44cc2c28756 (diff)
Warn `if` and `elsif` at EOL [EXPERIMENTAL]
It is unnatural and probably a typo.
Diffstat (limited to 'parse.y')
-rw-r--r--parse.y22
1 files changed, 22 insertions, 0 deletions
diff --git a/parse.y b/parse.y
index e1fe38e0d7..e957c063bc 100644
--- a/parse.y
+++ b/parse.y
@@ -952,6 +952,12 @@ static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_
static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
+
+#define WARN_EOL(tok) \
+ (looking_at_eol_p(p) ? \
+ rb_warning0("`" tok "' at the end of line without an expression") : \
+ (void)0)
+static int looking_at_eol_p(struct parser_params *p);
%}
%expect 0
@@ -3070,6 +3076,7 @@ k_begin : keyword_begin
k_if : keyword_if
{
+ WARN_EOL("if");
token_info_push(p, "if", &@$);
if (p->token_info && p->token_info->nonspc &&
p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
@@ -3179,6 +3186,7 @@ k_else : keyword_else
k_elsif : keyword_elsif
{
+ WARN_EOL("elisif");
token_info_warn(p, "elsif", p->token_info, 1, &@$);
}
;
@@ -6272,6 +6280,20 @@ pushback(struct parser_params *p, int c)
#define tok(p) (p)->tokenbuf
#define toklen(p) (p)->tokidx
+static int
+looking_at_eol_p(struct parser_params *p)
+{
+ int c;
+ while ((c = nextc(p)) != -1) {
+ int eol = (c == '\n' || c == '#');
+ if (eol || !ISSPACE(c)) {
+ pushback(p, c);
+ return eol;
+ }
+ }
+ return TRUE;
+}
+
static char*
newtok(struct parser_params *p)
{