]> ocean-lang.org Git - ocean/blobdiff - csrc/scanner.mdc
scanner: fix handling of indents in sub-nodes
[ocean] / csrc / scanner.mdc
index fa27a89524a85d4eeb9a16019558e61a993afb5f..21ec828c1d36dea273cabe05c3a3dce3844c29a2 100644 (file)
@@ -716,6 +716,8 @@ ignored.
        int     indent_level;
        int     indent_sizes[20];
 
+`indent_sizes[0]` will always be zero - this simplifies some code.
+
 #### Newlines
 
 Newlines can optionally be reported.  Newlines within a block comment
@@ -796,6 +798,14 @@ Separately we need, on each call to `token_next`, to check if
 there are some delayed tokens and if so we need to advance the state
 information and return one token.
 
+###### internal functions
+       static int state_indent(struct token_state *state)
+       {
+               if (state->node == NULL)
+                       return state->col;
+               return state->node->indent - state->node->needs_strip + state->col;
+       }
+
 ###### white space
        if (is_newline(ch) || (at_son(state) && ch <= ' ')) {
                int newlines = 0;
@@ -817,11 +827,6 @@ information and return one token.
                }
                if (at_eon(state)) {
                        newlines += 1;
-                       if (state->node->next &&
-                           state->node->next->indent > state->node->indent)
-                               state->col = state->node->next->indent;
-                       else
-                               state->col = state->node->indent;
                } else
                        unget_char(state);
                state->delayed_lines = newlines;
@@ -833,7 +838,7 @@ information and return one token.
 ###### delayed tokens
 
        if (state->check_indent || state->delayed_lines) {
-               if (state->col < state->indent_sizes[state->indent_level]) {
+               if (state_indent(state) < state->indent_sizes[state->indent_level]) {
                        if (!state->out_next &&
                            !(ignored & (1<<TK_newline))) {
                                state->out_next = 1;
@@ -845,10 +850,10 @@ information and return one token.
                        tk.num = TK_out;
                        return tk;
                }
-               if (state->col > state->indent_sizes[state->indent_level] &&
+               if (state_indent(state) > state->indent_sizes[state->indent_level] &&
                    state->indent_level < sizeof(state->indent_sizes)-1) {
                        state->indent_level += 1;
-                       state->indent_sizes[state->indent_level] = state->col;
+                       state->indent_sizes[state->indent_level] = state_indent(state);
                        state->delayed_lines -= 1;
                        tk.num = TK_in;
                        return tk;
@@ -938,7 +943,7 @@ a flag that tells us whether or not we need to strip.
 
 ###### internal functions
 
-       static int do_strip(struct token_state *state)
+       static void do_strip(struct token_state *state)
        {
                int indent = 0;
                if (state->node->needs_strip) {
@@ -954,7 +959,6 @@ a flag that tells us whether or not we need to strip.
                                n -= 4;
                        }
                }
-               return indent;
        }
 
        static wint_t get_char(struct token_state *state)
@@ -973,7 +977,8 @@ a flag that tells us whether or not we need to strip.
                        if (state->node == NULL)
                                return WEOF;
                        state->line = state->node->line_no;
-                       state->col = do_strip(state);
+                       do_strip(state);
+                       state->col = state->node->needs_strip;
                        state->strip_offset = state->offset;
                }
 
@@ -999,7 +1004,8 @@ a flag that tells us whether or not we need to strip.
                        state->col += 1;
                } else if (is_newline(next)) {
                        state->line += 1;
-                       state->col = do_strip(state);
+                       do_strip(state);
+                       state->col = state->node->needs_strip;
                } else if (next == '\t') {
                        state->col = indent_tab(state->col);
                }
@@ -1211,7 +1217,8 @@ As well as getting tokens, we need to be able to create the
                memset(state, 0, sizeof(*state));
                state->node = code;
                state->line = code->line_no;
-               state->col = do_strip(state);
+               do_strip(state);
+               state->col = state->node->needs_strip;
                state->strip_offset = state->offset;
                state->conf = conf;
                return state;