<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -31,31 +31,26 @@
 static
 void dump_stack(struct gzl_parse_state *s, FILE *output)
 {
-    fprintf(output, &quot;Stack dump:\n&quot;);
+    fprintf(output, &quot;Stack dump:&quot;);
     struct gzl_grammar *g = s-&gt;bound_grammar-&gt;grammar;
-    for(int i = 0; i &lt; s-&gt;parse_stack_len; i++)
-    {
+    for(int i = 0; i &lt; s-&gt;parse_stack_len; i++) {
         struct gzl_parse_stack_frame *frame = &amp;s-&gt;parse_stack[i];
-        switch(frame-&gt;frame_type)
-        {
-            case GZL_FRAME_TYPE_RTN:
-            {
+        switch(frame-&gt;frame_type) {
+            case GZL_FRAME_TYPE_RTN: {
                 struct gzl_rtn_frame *rtn_frame = &amp;frame-&gt;f.rtn_frame;
-                fprintf(output, &quot;RTN: %s&quot;, rtn_frame-&gt;rtn-&gt;name);
+                fprintf(output, &quot;RTN: %s, &quot;, rtn_frame-&gt;rtn-&gt;name);
                 break;
             }
 
-            case GZL_FRAME_TYPE_GLA:
-            {
+            case GZL_FRAME_TYPE_GLA: {
                 struct gzl_gla_frame *gla_frame = &amp;frame-&gt;f.gla_frame;
-                fprintf(output, &quot;GLA: #%d&quot;, gla_frame-&gt;gla - g-&gt;glas);
+                fprintf(output, &quot;GLA: #%d, &quot;, gla_frame-&gt;gla - g-&gt;glas);
                 break;
             }
 
-            case GZL_FRAME_TYPE_INTFA:
-            {
+            case GZL_FRAME_TYPE_INTFA: {
                 struct gzl_intfa_frame *intfa_frame = &amp;frame-&gt;f.intfa_frame;
-                fprintf(output, &quot;IntFA: #%d&quot;, intfa_frame-&gt;intfa - g-&gt;intfas);
+                fprintf(output, &quot;IntFA: #%d, &quot;, intfa_frame-&gt;intfa - g-&gt;intfas);
                 break;
             }
         }
@@ -63,6 +58,12 @@ void dump_stack(struct gzl_parse_state *s, FILE *output)
     fprintf(output, &quot;\n&quot;);
 }
 
+/*
+ * The following are stack-manipulation functions.  Gazelle maintains a runtime
+ * stack (which is completely separate from the C stack), and these functions
+ * provide pushing and popping of different kinds of stack frames.
+ */
+
 static
 struct gzl_parse_stack_frame *push_empty_frame(struct gzl_parse_state *s,
                                                enum gzl_frame_type frame_type,
@@ -76,55 +77,53 @@ struct gzl_parse_stack_frame *push_empty_frame(struct gzl_parse_state *s,
 }
 
 static
-struct gzl_intfa_frame *push_intfa_frame(struct gzl_parse_state *s, struct gzl_intfa *intfa,
+struct gzl_intfa_frame *push_intfa_frame(struct gzl_parse_state *s,
+                                         struct gzl_intfa *intfa,
                                          struct gzl_offset *start_offset)
 {
-    struct gzl_parse_stack_frame *frame = push_empty_frame(s, GZL_FRAME_TYPE_INTFA, start_offset);
+    struct gzl_parse_stack_frame *frame =
+        push_empty_frame(s, GZL_FRAME_TYPE_INTFA, start_offset);
     struct gzl_intfa_frame *intfa_frame = &amp;frame-&gt;f.intfa_frame;
     intfa_frame-&gt;intfa        = intfa;
     intfa_frame-&gt;intfa_state  = &amp;intfa-&gt;states[0];
-
     return intfa_frame;
 }
 
 static
-struct gzl_parse_stack_frame *push_gla_frame(struct gzl_parse_state *s, struct gzl_gla *gla,
+struct gzl_parse_stack_frame *push_gla_frame(struct gzl_parse_state *s,
+                                             struct gzl_gla *gla,
                                              struct gzl_offset *start_offset)
 {
-    struct gzl_parse_stack_frame *frame = push_empty_frame(s, GZL_FRAME_TYPE_GLA, start_offset);
+    struct gzl_parse_stack_frame *frame =
+        push_empty_frame(s, GZL_FRAME_TYPE_GLA, start_offset);
     struct gzl_gla_frame *gla_frame = &amp;frame-&gt;f.gla_frame;
     gla_frame-&gt;gla          = gla;
     gla_frame-&gt;gla_state    = &amp;gla-&gt;states[0];
-
     return frame;
 }
 
 static
-enum gzl_status push_rtn_frame(struct gzl_parse_state *s, struct gzl_rtn *rtn,
-                                     struct gzl_offset *start_offset)
+enum gzl_status push_rtn_frame(struct gzl_parse_state *s,
+                               struct gzl_rtn *rtn,
+                               struct gzl_offset *start_offset)
 {
-    struct gzl_parse_stack_frame *new_frame = push_empty_frame(s, GZL_FRAME_TYPE_RTN, start_offset);
+    struct gzl_parse_stack_frame *new_frame =
+        push_empty_frame(s, GZL_FRAME_TYPE_RTN, start_offset);
     struct gzl_rtn_frame *new_rtn_frame = &amp;new_frame-&gt;f.rtn_frame;
-
     new_rtn_frame-&gt;rtn            = rtn;
     new_rtn_frame-&gt;rtn_transition = NULL;
     new_rtn_frame-&gt;rtn_state      = &amp;new_rtn_frame-&gt;rtn-&gt;states[0];
-
-    /* Call start rule callback if set */
-    if(s-&gt;bound_grammar-&gt;start_rule_cb)
-    {
-        s-&gt;bound_grammar-&gt;start_rule_cb(s);
-    }
-
+    if(s-&gt;bound_grammar-&gt;start_rule_cb) s-&gt;bound_grammar-&gt;start_rule_cb(s);
     return GZL_STATUS_OK;
 }
 
 static
 enum gzl_status push_rtn_frame_for_transition(struct gzl_parse_state *s,
-                                                    struct gzl_rtn_transition *t,
-                                                    struct gzl_offset *start_offset)
+                                              struct gzl_rtn_transition *t,
+                                              struct gzl_offset *start_offset)
 {
-    struct gzl_rtn_frame *old_rtn_frame = &amp;DYNARRAY_GET_TOP(s-&gt;parse_stack)-&gt;f.rtn_frame;
+    struct gzl_rtn_frame *old_rtn_frame =
+        &amp;DYNARRAY_GET_TOP(s-&gt;parse_stack)-&gt;f.rtn_frame;
     old_rtn_frame-&gt;rtn_transition = t;
     return push_rtn_frame(s, t-&gt;edge.nonterminal, start_offset);
 }
@@ -134,47 +133,28 @@ struct gzl_parse_stack_frame *pop_frame(struct gzl_parse_state *s)
 {
     assert(s-&gt;parse_stack_len &gt; 0);
     RESIZE_DYNARRAY(s-&gt;parse_stack, s-&gt;parse_stack_len-1);
-
-    struct gzl_parse_stack_frame *frame;
-    if(s-&gt;parse_stack_len &gt; 0)
-        frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
-    else
-        frame = NULL;
-
-    return frame;
+    return s-&gt;parse_stack_len &gt; 0 ? DYNARRAY_GET_TOP(s-&gt;parse_stack) : NULL;
 }
 
 static
 enum gzl_status pop_rtn_frame(struct gzl_parse_state *s)
 {
     assert(DYNARRAY_GET_TOP(s-&gt;parse_stack)-&gt;frame_type == GZL_FRAME_TYPE_RTN);
-
-    /* Call end rule callback if set */
-    if(s-&gt;bound_grammar-&gt;end_rule_cb)
-    {
-        s-&gt;bound_grammar-&gt;end_rule_cb(s);
-    }
+    if(s-&gt;bound_grammar-&gt;end_rule_cb) s-&gt;bound_grammar-&gt;end_rule_cb(s);
 
     struct gzl_parse_stack_frame *frame = pop_frame(s);
-    if(frame == NULL)
-    {
-        return GZL_STATUS_HARD_EOF;
-    }
-    else
-    {
+    if(frame) {
         assert(frame-&gt;frame_type == GZL_FRAME_TYPE_RTN);
         struct gzl_rtn_frame *rtn_frame = &amp;frame-&gt;f.rtn_frame;
         if(rtn_frame-&gt;rtn_transition)
-        {
             rtn_frame-&gt;rtn_state = rtn_frame-&gt;rtn_transition-&gt;dest_state;
-        }
-        else
-        {
-          // Should only happen at the top level.
+        else {
+          /* Should only happen at the top level. */
           assert(s-&gt;parse_stack_len == 1);
         }
         return GZL_STATUS_OK;
-    }
+    } else
+        return GZL_STATUS_HARD_EOF;
 }
 
 static
@@ -187,18 +167,11 @@ struct gzl_parse_stack_frame *pop_gla_frame(struct gzl_parse_state *s)
 static
 struct gzl_parse_stack_frame *pop_intfa_frame(struct gzl_parse_state *s)
 {
-    assert(DYNARRAY_GET_TOP(s-&gt;parse_stack)-&gt;frame_type == GZL_FRAME_TYPE_INTFA);
+    assert(DYNARRAY_GET_TOP(s-&gt;parse_stack)-&gt;frame_type ==
+           GZL_FRAME_TYPE_INTFA);
     return pop_frame(s);
 }
 
-static
-struct gzl_intfa_frame *get_top_intfa_frame(struct gzl_parse_state *s)
-{
-    struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
-    assert(frame-&gt;frame_type == GZL_FRAME_TYPE_INTFA);
-    return &amp;frame-&gt;f.intfa_frame;
-}
-
 /*
  * descend_to_gla(): given the current parse stack, pushes any RTN or GLA
  * stack frames representing transitions that can be taken without consuming
@@ -213,15 +186,12 @@ struct gzl_intfa_frame *get_top_intfa_frame(struct gzl_parse_state *s)
  */
 static
 enum gzl_status descend_to_gla(struct gzl_parse_state *s, bool *entered_gla,
-                                     struct gzl_offset *start_offset)
+                               struct gzl_offset *start_offset)
 {
     *entered_gla = false;
-    while(true)
-    {
+    while(true) {
         struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
-
-        if(frame-&gt;frame_type != GZL_FRAME_TYPE_RTN)
-            return GZL_STATUS_OK;
+        if(frame-&gt;frame_type != GZL_FRAME_TYPE_RTN) return GZL_STATUS_OK;
 
         /* Subtract 1 because there can be one IntFA frame beyond the RTN and
          * GLA frames this function pushes. */
@@ -229,8 +199,7 @@ enum gzl_status descend_to_gla(struct gzl_parse_state *s, bool *entered_gla,
             return GZL_STATUS_RESOURCE_LIMIT_EXCEEDED;
 
         struct gzl_rtn_frame *rtn_frame = &amp;frame-&gt;f.rtn_frame;
-        switch(rtn_frame-&gt;rtn_state-&gt;lookahead_type)
-        {
+        switch(rtn_frame-&gt;rtn_state-&gt;lookahead_type) {
           case GZL_STATE_HAS_INTFA:
             return GZL_STATUS_OK;
 
@@ -242,90 +211,76 @@ enum gzl_status descend_to_gla(struct gzl_parse_state *s, bool *entered_gla,
           case GZL_STATE_HAS_NEITHER:
             /* An RTN state has neither an IntFA or a GLA in only two cases:
              * - it is a final state with no outgoing transitions
-             * - it is a nonfinal state with only one transition (a nonterminal) */
+             * - it is a nonfinal state with only one transition (a nonterminal)
+             */
             assert(rtn_frame-&gt;rtn_state-&gt;num_transitions &lt; 2);
             enum gzl_status status = GZL_STATUS_OK;
             if(rtn_frame-&gt;rtn_state-&gt;num_transitions == 0)
-            {
-                /* Final state */
-                status = pop_rtn_frame(s);
-            }
-            else if(rtn_frame-&gt;rtn_state-&gt;num_transitions == 1)
-            {
-                assert(rtn_frame-&gt;rtn_state-&gt;transitions[0].transition_type == GZL_NONTERM_TRANSITION);
-                status = push_rtn_frame_for_transition(s, &amp;rtn_frame-&gt;rtn_state-&gt;transitions[0],
-                                                       start_offset);
+                status = pop_rtn_frame(s); /* Final state */
+            else if(rtn_frame-&gt;rtn_state-&gt;num_transitions == 1) {
+                assert(rtn_frame-&gt;rtn_state-&gt;transitions[0].transition_type ==
+                       GZL_NONTERM_TRANSITION);
+                status = push_rtn_frame_for_transition(
+                    s, &amp;rtn_frame-&gt;rtn_state-&gt;transitions[0], start_offset);
             }
-            if(status != GZL_STATUS_OK)
-                return status;
+            if(status != GZL_STATUS_OK) return status;
             break;
         }
     }
 }
 
 static
-struct gzl_intfa_frame *push_intfa_frame_for_gla_or_rtn(struct gzl_parse_state *s)
+struct gzl_intfa_frame *push_intfa_frame_for_gla_or_rtn(
+    struct gzl_parse_state *s)
 {
     struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
-    if(frame-&gt;frame_type == GZL_FRAME_TYPE_GLA)
-    {
-        assert(frame-&gt;f.gla_frame.gla_state-&gt;is_final == false);
-        return push_intfa_frame(s, frame-&gt;f.gla_frame.gla_state-&gt;d.nonfinal.intfa, &amp;s-&gt;offset);
+    if(frame-&gt;frame_type == GZL_FRAME_TYPE_GLA) {
+        struct gzl_gla_state *gla_state = frame-&gt;f.gla_frame.gla_state;
+        assert(gla_state-&gt;is_final == false);
+        return push_intfa_frame(s, gla_state-&gt;d.nonfinal.intfa, &amp;s-&gt;offset);
+    } else if(frame-&gt;frame_type == GZL_FRAME_TYPE_RTN) {
+        struct gzl_rtn_state *rtn_state = frame-&gt;f.rtn_frame.rtn_state;
+        assert(rtn_state-&gt;lookahead_type == GZL_STATE_HAS_INTFA);
+        return push_intfa_frame(s, rtn_state-&gt;d.state_intfa, &amp;s-&gt;offset);
     }
-    else if(frame-&gt;frame_type == GZL_FRAME_TYPE_RTN)
-    {
-        return push_intfa_frame(s, frame-&gt;f.rtn_frame.rtn_state-&gt;d.state_intfa, &amp;s-&gt;offset);
-    }
-    assert(false);
+    assert(false);  /* should never reach here. */
     return NULL;
 }
 
 static
 enum gzl_status do_rtn_terminal_transition(struct gzl_parse_state *s,
-                                                 struct gzl_rtn_transition *t,
-                                                 struct gzl_terminal *terminal)
+                                           struct gzl_rtn_transition *t,
+                                           struct gzl_terminal *terminal)
 {
     struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
     assert(frame-&gt;frame_type == GZL_FRAME_TYPE_RTN);
     struct gzl_rtn_frame *rtn_frame = &amp;frame-&gt;f.rtn_frame;
-
-    /* Call terminal callback if set */
+    rtn_frame-&gt;rtn_transition = t;
     if(s-&gt;bound_grammar-&gt;terminal_cb)
-    {
-        rtn_frame-&gt;rtn_transition = t;
-        s-&gt;bound_grammar-&gt;terminal_cb(s, terminal);
-    }
-
+      s-&gt;bound_grammar-&gt;terminal_cb(s, terminal);
     assert(t-&gt;transition_type == GZL_TERMINAL_TRANSITION);
     rtn_frame-&gt;rtn_state = t-&gt;dest_state;
     return GZL_STATUS_OK;
 }
 
 static
-struct gzl_rtn_transition *find_rtn_terminal_transition(struct gzl_parse_state *s,
-                                                        struct gzl_terminal *terminal)
+struct gzl_rtn_transition *find_rtn_terminal_transition(
+    struct gzl_rtn_state *rtn_state, struct gzl_terminal *terminal)
 {
-    struct gzl_parse_stack_frame *frame = &amp;s-&gt;parse_stack[s-&gt;parse_stack_len-1];
-    struct gzl_rtn_frame *rtn_frame = &amp;frame-&gt;f.rtn_frame;
-    for(int i = 0; i &lt; rtn_frame-&gt;rtn_state-&gt;num_transitions; i++)
-    {
-        struct gzl_rtn_transition *t = &amp;rtn_frame-&gt;rtn_state-&gt;transitions[i];
-        if(t-&gt;transition_type == GZL_TERMINAL_TRANSITION &amp;&amp; t-&gt;edge.terminal_name == terminal-&gt;name)
-        {
+    for(int i = 0; i &lt; rtn_state-&gt;num_transitions; i++) {
+        struct gzl_rtn_transition *t = &amp;rtn_state-&gt;transitions[i];
+        if(t-&gt;transition_type == GZL_TERMINAL_TRANSITION &amp;&amp;
+           t-&gt;edge.terminal_name == terminal-&gt;name)
             return t;
-        }
     }
-
     return NULL;
 }
 
-/* term_name can be NULL if we're looking for EOF. */
 static
 struct gzl_gla_transition *find_gla_transition(struct gzl_gla_state *gla_state,
                                                char *term_name)
 {
-    for(int i = 0; i &lt; gla_state-&gt;d.nonfinal.num_transitions; i++)
-    {
+    for(int i = 0; i &lt; gla_state-&gt;d.nonfinal.num_transitions; i++) {
         struct gzl_gla_transition *t = &amp;gla_state-&gt;d.nonfinal.transitions[i];
         if(t-&gt;term == term_name)
             return t;
@@ -333,6 +288,18 @@ struct gzl_gla_transition *find_gla_transition(struct gzl_gla_state *gla_state,
     return NULL;
 }
 
+static
+struct gzl_intfa_transition *find_intfa_transition(
+    struct gzl_intfa_state *intfa_state, char ch)
+{
+    for(int i = 0; i &lt; intfa_state-&gt;num_transitions; i++) {
+        struct gzl_intfa_transition *t = &amp;intfa_state-&gt;transitions[i];
+        if(ch &gt;= t-&gt;ch_low &amp;&amp; ch &lt;= t-&gt;ch_high)
+            return t;
+    }
+    return NULL;
+}
+
 /*
  * do_gla_transition(): transitions a GLA frame, performing the appropriate
  * RTN transitions if this puts the GLA in a final state.
@@ -351,53 +318,48 @@ enum gzl_status do_gla_transition(struct gzl_parse_state *s,
                                         struct gzl_terminal *term,
                                         int *rtn_term_offset)
 {
-    struct gzl_parse_stack_frame *frame = &amp;s-&gt;parse_stack[s-&gt;parse_stack_len-1];
+    struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
     assert(frame-&gt;frame_type == GZL_FRAME_TYPE_GLA);
     assert(frame-&gt;f.gla_frame.gla_state-&gt;is_final == false);
     struct gzl_gla_state *gla_state = frame-&gt;f.gla_frame.gla_state;
     struct gzl_gla_state *dest_gla_state = NULL;
 
+    /* Find the transition. */
     struct gzl_gla_transition *t = find_gla_transition(gla_state, term-&gt;name);
-    if(!t)
-    {
+    if(!t) {
+        /* Parse error: terminal for which we had no GLA transition. */
         if(s-&gt;bound_grammar-&gt;error_terminal_cb)
             s-&gt;bound_grammar-&gt;error_terminal_cb(s, term);
         return GZL_STATUS_ERROR;
     }
+    /* Perform the transition. */
     assert(t-&gt;dest_state);
     frame-&gt;f.gla_frame.gla_state = t-&gt;dest_state;
     dest_gla_state = t-&gt;dest_state;
 
+    /* Perform appropriate actions if we're in a final state. */
     enum gzl_status status = GZL_STATUS_OK;
-
-    if(dest_gla_state-&gt;is_final)
-    {
-        /* pop the GLA frame (since now we know what RTN transition to take)
-         * and use its information to make an RTN transition */
+    if(dest_gla_state-&gt;is_final) {
+        /* Pop the GLA frame (since now we know what RTN transition to take)
+         * and use its information to make an RTN transition. */
         int offset = dest_gla_state-&gt;d.final.transition_offset;
         frame = pop_gla_frame(s);
         if(offset == 0)
-        {
             status = pop_rtn_frame(s);
-        }
-        else
-        {
-            struct gzl_rtn_transition *t = &amp;frame-&gt;f.rtn_frame.rtn_state-&gt;transitions[offset-1];
+        else {
+            struct gzl_rtn_state *rtn_state = frame-&gt;f.rtn_frame.rtn_state;
+            struct gzl_rtn_transition *t = &amp;rtn_state-&gt;transitions[offset-1];
             struct gzl_terminal *next_term = &amp;s-&gt;token_buffer[*rtn_term_offset];
-            if(t-&gt;transition_type == GZL_TERMINAL_TRANSITION)
-            {
+            if(t-&gt;transition_type == GZL_TERMINAL_TRANSITION) {
                 /* The transition must match what we have in the token buffer */
-                (*rtn_term_offset)++;
                 assert(next_term-&gt;name == t-&gt;edge.terminal_name);
+                (*rtn_term_offset)++;
                 status = do_rtn_terminal_transition(s, t, next_term);
-            }
-            else
-            {
-                status = push_rtn_frame_for_transition(s, t, &amp;next_term-&gt;offset);
-            }
+            } else
+                status = push_rtn_frame_for_transition(
+                    s, t, &amp;next_term-&gt;offset);
         }
     }
-
     return status;
 }
 
@@ -416,13 +378,10 @@ enum gzl_status do_gla_transition(struct gzl_parse_state *s,
  */
 
 static
-enum gzl_status process_terminal(struct gzl_parse_state *s,
-                                       char *term_name,
-                                       struct gzl_offset *start_offset,
-                                       int len)
+enum gzl_status process_terminal(struct gzl_parse_state *s, char *term_name,
+                                 struct gzl_offset *start_offset, int len)
 {
     pop_intfa_frame(s);
-
     struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
     int rtn_term_offset = 0;
     int gla_term_offset = s-&gt;token_buffer_len;
@@ -436,77 +395,77 @@ enum gzl_status process_terminal(struct gzl_parse_state *s,
     term-&gt;offset = *start_offset;
     term-&gt;len = len;
 
-    /* Feed tokens to RTNs and GLAs until we have processed all the tokens we have */
+    /* Feed tokens to RTNs and GLAs until we have processed all the tokens we
+     * have. */
     enum gzl_status status = GZL_STATUS_OK;
     enum gzl_frame_type frame_type = frame-&gt;frame_type;
-    do
-    {
-        struct gzl_terminal *rtn_term = &amp;s-&gt;token_buffer[rtn_term_offset];
-        if(frame_type == GZL_FRAME_TYPE_RTN)
-        {
+    do {
+        /* Take one terminal transition, for either an RTN or a GLA. */
+        if(frame_type == GZL_FRAME_TYPE_RTN) {
+            struct gzl_terminal *rtn_term = &amp;s-&gt;token_buffer[rtn_term_offset];
             struct gzl_rtn_transition *t;
             rtn_term_offset++;
 
             if(rtn_term-&gt;name == NULL)
-            {
                 /* Skip: RTNs don't process EOF as a terminal, only GLAs do. */
                 continue;
-            }
-            t = find_rtn_terminal_transition(s, rtn_term);
-            if(!t)
-            {
+            t = find_rtn_terminal_transition(frame-&gt;f.rtn_frame.rtn_state,
+                                             rtn_term);
+            if(!t) {
+                /* Parse error: terminal for which we had no RTN transition. */
                 if(s-&gt;bound_grammar-&gt;error_terminal_cb)
                     s-&gt;bound_grammar-&gt;error_terminal_cb(s, term);
-
                 return GZL_STATUS_ERROR;
             }
-
             status = do_rtn_terminal_transition(s, t, rtn_term);
-        }
-        else
-        {
+        } else {
             struct gzl_terminal *gla_term = &amp;s-&gt;token_buffer[gla_term_offset++];
             status = do_gla_transition(s, gla_term, &amp;rtn_term_offset);
         }
 
-        if(status == GZL_STATUS_OK)
-        {
+        /* Having taken a transition, push any new frames onto the stack. */
+        if(status == GZL_STATUS_OK) {
             bool entered_gla;
             if(rtn_term_offset &lt; s-&gt;token_buffer_len)
-              status = descend_to_gla(s, &amp;entered_gla, &amp;s-&gt;token_buffer[rtn_term_offset].offset);
+                status = descend_to_gla(
+                    s, &amp;entered_gla, &amp;s-&gt;token_buffer[rtn_term_offset].offset);
             else
-              status = descend_to_gla(s, &amp;entered_gla, &amp;s-&gt;offset);
+                status = descend_to_gla(s, &amp;entered_gla, &amp;s-&gt;offset);
 
             if(entered_gla)
-            {
                 gla_term_offset = rtn_term_offset;
-            }
         }
 
-        if(status == GZL_STATUS_OK)
-        {
+        if(status == GZL_STATUS_OK) {
             assert(s-&gt;parse_stack_len &gt; 0);
-            frame_type = DYNARRAY_GET_TOP(s-&gt;parse_stack)-&gt;frame_type;
+            frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
+            frame_type = frame-&gt;frame_type;
         }
     }
     while(status == GZL_STATUS_OK &amp;&amp;
-          ((frame_type == GZL_FRAME_TYPE_RTN &amp;&amp; rtn_term_offset &lt; s-&gt;token_buffer_len) ||
-           (frame_type == GZL_FRAME_TYPE_GLA &amp;&amp; gla_term_offset &lt; s-&gt;token_buffer_len)));
+          ((frame_type == GZL_FRAME_TYPE_RTN &amp;&amp;
+            rtn_term_offset &lt; s-&gt;token_buffer_len) ||
+           (frame_type == GZL_FRAME_TYPE_GLA &amp;&amp;
+            gla_term_offset &lt; s-&gt;token_buffer_len)));
 
     /* We can have an EOF left over in the token buffer if the EOF token led us
-     * to a hard EOF, thus terminating the above loop before our &quot;skip&quot; above could
-     * cover this EOF special case. */
+     * to a hard EOF, thus terminating the above loop before our &quot;skip&quot; above
+     * could cover this EOF special case. */
     if(rtn_term_offset &lt; s-&gt;token_buffer_len &amp;&amp;
        s-&gt;token_buffer[rtn_term_offset].name == NULL)
         rtn_term_offset++;
 
-    /* Remove consumed terminals from token_buffer */
+    /* At this point we have consumed some (but possibly not all) of the
+     * terminals we have lexed.  We consider a token fully consumed when it
+     * has caused an RTN transition (just a GLA transition doesn't leave the
+     * token consumed, because it will be used again for an RTN transition
+     * later.
+     *
+     * We now remove the consumed terminals from token_buffer. */
     int remaining_terminals = s-&gt;token_buffer_len - rtn_term_offset;
     if(remaining_terminals &gt; 0)
-    {
         memmove(s-&gt;token_buffer, s-&gt;token_buffer + rtn_term_offset,
                 remaining_terminals * sizeof(*s-&gt;token_buffer));
-    }
     RESIZE_DYNARRAY(s-&gt;token_buffer, remaining_terminals);
 
     /* Update open_terminal_offset. */
@@ -518,24 +477,6 @@ enum gzl_status process_terminal(struct gzl_parse_state *s,
     return status;
 }
 
-/*
- * find_intfa_transition(): get the transition (if any) out of this state
- * on this character.
- */
-static
-struct gzl_intfa_transition *find_intfa_transition(struct gzl_intfa_frame *frame, char ch)
-{
-    for(int i = 0; i &lt; frame-&gt;intfa_state-&gt;num_transitions; i++)
-    {
-        struct gzl_intfa_transition *t = &amp;frame-&gt;intfa_state-&gt;transitions[i];
-        if(ch &gt;= t-&gt;ch_low &amp;&amp; ch &lt;= t-&gt;ch_high)
-        {
-            return t;
-        }
-    }
-
-    return NULL;
-}
 
 /*
  * do_intfa_transition(): transitions an IntFA frame according to the given
@@ -555,31 +496,28 @@ struct gzl_intfa_transition *find_intfa_transition(struct gzl_intfa_frame *frame
  */
 static
 enum gzl_status do_intfa_transition(struct gzl_parse_state *s,
-                                          char ch)
+                                    char ch)
 {
-    struct gzl_intfa_frame *intfa_frame = get_top_intfa_frame(s);
-    struct gzl_intfa_transition *t = find_intfa_transition(intfa_frame, ch);
-    struct gzl_parse_stack_frame *frame = GET_PARSE_STACK_FRAME(intfa_frame);
+    struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
+    assert(frame-&gt;frame_type == GZL_FRAME_TYPE_INTFA);
+    struct gzl_intfa_frame *intfa_frame = &amp;frame-&gt;f.intfa_frame;
+    struct gzl_intfa_transition *t = find_intfa_transition(
+        intfa_frame-&gt;intfa_state, ch);
     enum gzl_status status;
 
     /* If this character did not have any transition, but the state we're coming
      * from is final, then longest-match semantics say that we should return
      * the last character's final state as the token.  But if the state we're
      * coming from is *not* final, it's just a parse error. */
-    if(!t)
-    {
+    if(!t) {
         char *terminal = intfa_frame-&gt;intfa_state-&gt;final;
-        assert(terminal);
+        assert(terminal);  /* TODO: handle this case. */
         status = process_terminal(s, terminal, &amp;frame-&gt;start_offset,
                                   s-&gt;offset.byte - frame-&gt;start_offset.byte);
-        if(status != GZL_STATUS_OK)
-            return status;
-
+        if(status != GZL_STATUS_OK) return status;
         intfa_frame = push_intfa_frame_for_gla_or_rtn(s);
-        t = find_intfa_transition(intfa_frame, ch);
-
-        if(!t)
-        {
+        t = find_intfa_transition(intfa_frame-&gt;intfa_state, ch);
+        if(!t) {
             /* Parse error: we encountered a character for which we have no
              * transition. */
             if(s-&gt;bound_grammar-&gt;error_char_cb)
@@ -588,48 +526,44 @@ enum gzl_status do_intfa_transition(struct gzl_parse_state *s,
         }
     }
 
-    /* We increment the offset here because we have just crossed the threshold
-     * where we have finished processing all terminals for the previous byte and
-     * started processing transitions for the current byte. */
+    /* We have finished processing transitions for the previous byte.
+     * Move on to the next byte. */
     s-&gt;offset.byte++;
 
     /* Deal with newlines.  This is all very single-byte-encoding specific for
      * the moment. */
     bool is_newline_char = (ch == 0x0A || ch == 0x0D);  /* LF and CR */
-    if(is_newline_char)
-    {
-        if(!s-&gt;last_char_was_newline)
-        {
+    if(is_newline_char) {
+        if(!s-&gt;last_char_was_newline) {
             s-&gt;offset.line++;
             s-&gt;offset.column = 1;
         }
     }
     else
         s-&gt;offset.column++;
-
     s-&gt;last_char_was_newline = is_newline_char;
 
+    /* Do the transition. */
     intfa_frame-&gt;intfa_state = t-&gt;dest_state;
 
     /* If the current state is final and there are no outgoing transitions,
      * we *know* we don't have to wait any longer for the longest match.
      * Transition the RTN or GLA now, for more on-line behavior. */
-    if(intfa_frame-&gt;intfa_state-&gt;final &amp;&amp; (intfa_frame-&gt;intfa_state-&gt;num_transitions == 0))
-    {
+    if(intfa_frame-&gt;intfa_state-&gt;final &amp;&amp;
+       (intfa_frame-&gt;intfa_state-&gt;num_transitions == 0)) {
         status = process_terminal(s, intfa_frame-&gt;intfa_state-&gt;final,
                                   &amp;frame-&gt;start_offset,
                                   s-&gt;offset.byte - frame-&gt;start_offset.byte);
         if(status != GZL_STATUS_OK)
             return status;
-
         push_intfa_frame_for_gla_or_rtn(s);
     }
-
     return GZL_STATUS_OK;
 }
 
 /*
- * The rest of this file is the publicly-exposed API
+ * The rest of this file is the publicly-exposed API, documented in the
+ * header file.
  */
 
 enum gzl_status gzl_parse(struct gzl_parse_state *s, char *buf, size_t buf_len)
@@ -638,23 +572,19 @@ enum gzl_status gzl_parse(struct gzl_parse_state *s, char *buf, size_t buf_len)
 
     /* For the first call, we need to push the initial frame and
      * descend from the starting frame until we hit an IntFA frame. */
-    if(s-&gt;offset.byte == 0 &amp;&amp; s-&gt;parse_stack_len == 0)
-    {
+    if(s-&gt;offset.byte == 0 &amp;&amp; s-&gt;parse_stack_len == 0) {
         push_rtn_frame(s, &amp;s-&gt;bound_grammar-&gt;grammar-&gt;rtns[0], &amp;s-&gt;offset);
         bool entered_gla;
         status = descend_to_gla(s, &amp;entered_gla, &amp;s-&gt;offset);
-        if(status == GZL_STATUS_OK)
-            push_intfa_frame_for_gla_or_rtn(s);
+        if(status == GZL_STATUS_OK) push_intfa_frame_for_gla_or_rtn(s);
     }
-    if(s-&gt;parse_stack_len == 0)
-    {
+    if(s-&gt;parse_stack_len == 0) {
         /* This gzl_parse_state has already hit hard EOF previously. */
         return GZL_STATUS_HARD_EOF;
     }
 
     for(int i = 0; i &lt; buf_len &amp;&amp; status == GZL_STATUS_OK; i++)
         status = do_intfa_transition(s, buf[i]);
-
     return status;
 }
 
@@ -664,32 +594,24 @@ bool gzl_finish_parse(struct gzl_parse_state *s)
      * be in a start state (in which case we back it out), a final state
      * (in which case we recognize and process the terminal), or both (in
      * which case we back out iff. we are in a GLA state with an EOF transition
-     * out).
-     */
+     * out).  */
     struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
-    if(frame-&gt;frame_type == GZL_FRAME_TYPE_INTFA)
-    {
+    if(frame-&gt;frame_type == GZL_FRAME_TYPE_INTFA) {
         struct gzl_intfa_frame *intfa_frame = &amp;frame-&gt;f.intfa_frame;
         if(intfa_frame-&gt;intfa_state-&gt;final &amp;&amp;
-           intfa_frame-&gt;intfa_state == &amp;intfa_frame-&gt;intfa-&gt;states[0])
-        {
-            /* the hard case: we don't handle it yet. */
+           intfa_frame-&gt;intfa_state == &amp;intfa_frame-&gt;intfa-&gt;states[0]) {
+            /* TODO: handle this case. */
             assert(false);
-        }
-        else if(intfa_frame-&gt;intfa_state-&gt;final)
-        {
+        } else if(intfa_frame-&gt;intfa_state-&gt;final) {
             process_terminal(s, intfa_frame-&gt;intfa_state-&gt;final,
                              &amp;frame-&gt;start_offset,
                              s-&gt;offset.byte - frame-&gt;start_offset.byte);
-        }
-        else if(intfa_frame-&gt;intfa_state == &amp;intfa_frame-&gt;intfa-&gt;states[0])
-        {
+        } else if(intfa_frame-&gt;intfa_state == &amp;intfa_frame-&gt;intfa-&gt;states[0]) {
             /* Pop the frame like it never happened. */
             pop_intfa_frame(s);
-        }
-        else
-        {
-            /* IntFA is in neither a start nor a final state.  This cannot be EOF. */
+        } else {
+            /* IntFA is in neither a start nor a final state. 
+             * This cannot be EOF. */
             return false;
         }
     }
@@ -698,30 +620,27 @@ bool gzl_finish_parse(struct gzl_parse_state *s)
      * a start state or have an outgoing EOF transition, else we are not at
      * valid EOF. */
     frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
-    if(frame-&gt;frame_type == GZL_FRAME_TYPE_GLA)
-    {
+    if(frame-&gt;frame_type == GZL_FRAME_TYPE_GLA) {
         struct gzl_gla_frame *gla_frame = &amp;frame-&gt;f.gla_frame;
-        if(gla_frame-&gt;gla_state == &amp;gla_frame-&gt;gla-&gt;states[0])
-        {
+        if(gla_frame-&gt;gla_state == &amp;gla_frame-&gt;gla-&gt;states[0]) {
             /* GLA is in a start state -- fine, we can just pop it as
              * if it never happened. */
             pop_gla_frame(s);
-        }
-        else
-        {
+        } else {
             /* For this to still be valid EOF, this GLA state must have an
              * outgoing EOF transition, and we must take it now. */
-            struct gzl_gla_transition *t = find_gla_transition(gla_frame-&gt;gla_state, NULL);
-            if(!t)
-                return false;
+            struct gzl_gla_transition *t =
+                find_gla_transition(gla_frame-&gt;gla_state, NULL);
+            if(!t) return false;
 
-            /* process_terminal wants an IntFA frame to pop. */
+            /* process_terminal() wants an IntFA frame to pop. */
             push_empty_frame(s, GZL_FRAME_TYPE_INTFA, &amp;s-&gt;offset);
             process_terminal(s, NULL, &amp;s-&gt;offset, 0);
 
             /* Pop any GLA states that the previous may have pushed. */
             while(s-&gt;parse_stack_len &gt; 0 &amp;&amp;
-                  DYNARRAY_GET_TOP(s-&gt;parse_stack)-&gt;frame_type != GZL_FRAME_TYPE_RTN)
+                  DYNARRAY_GET_TOP(s-&gt;parse_stack)-&gt;frame_type !=
+                  GZL_FRAME_TYPE_RTN)
                 pop_frame(s);
         }
     }
@@ -729,19 +648,18 @@ bool gzl_finish_parse(struct gzl_parse_state *s)
     /* Now we should have only RTN frames open.  Starting from the top, check
      * that each frame's dest_state is a final state (or the actual current
      * state in the bottommost frame). */
-    if(s-&gt;parse_stack_len &gt; 0)  // will be 0 (no open frames) if we already hit hard EOF.
-    {
-        for(int i = 0; i &lt; s-&gt;parse_stack_len - 1; i++)
-        {
-            struct gzl_rtn_frame *rtn_frame = &amp;s-&gt;parse_stack[i].f.rtn_frame;
+    if(s-&gt;parse_stack_len &gt; 0) { /* will be 0 if we already hit hard EOF. */
+        for(int i = 0; i &lt; s-&gt;parse_stack_len - 1; i++) {
+            frame = &amp;s-&gt;parse_stack[i];
+            assert(frame-&gt;frame_type == GZL_FRAME_TYPE_RTN);
+            struct gzl_rtn_frame *rtn_frame = &amp;frame-&gt;f.rtn_frame;
             assert(rtn_frame-&gt;rtn_transition);
-            if(!rtn_frame-&gt;rtn_transition-&gt;dest_state-&gt;is_final)
-                return false;
+            if(!rtn_frame-&gt;rtn_transition-&gt;dest_state-&gt;is_final) return false;
         }
 
-        struct gzl_rtn_frame *rtn_frame = &amp;s-&gt;parse_stack[s-&gt;parse_stack_len-1].f.rtn_frame;
-        if(!rtn_frame-&gt;rtn_state-&gt;is_final)
-            return false;
+        frame = DYNARRAY_GET_TOP(s-&gt;parse_stack);
+        struct gzl_rtn_frame *rtn_frame = &amp;frame-&gt;f.rtn_frame;
+        if(!rtn_frame-&gt;rtn_state-&gt;is_final) return false;
 
         /* We are truly in a state where EOF is ok.  Pop remaining RTN frames to
          * call callbacks appropriately. */
@@ -768,13 +686,16 @@ struct gzl_parse_state *gzl_alloc_parse_state()
 
 struct gzl_parse_state *gzl_dup_parse_state(struct gzl_parse_state *orig)
 {
-    struct gzl_parse_state *copy = gzl_alloc_parse_state();
-    *copy = *orig;  // erroneously copies pointers to dynarrays, but we'll fix in a sec.
+    struct gzl_parse_state *copy = malloc(sizeof(*copy));
+    /* This erroneously copies pointers to dynarrays, but we'll fix in a sec. */
+    *copy = *orig;
 
+    INIT_DYNARRAY(copy-&gt;parse_stack, 0, 16);
     RESIZE_DYNARRAY(copy-&gt;parse_stack, orig-&gt;parse_stack_len);
     for(int i = 0; i &lt; orig-&gt;parse_stack_len; i++)
         copy-&gt;parse_stack[i] = orig-&gt;parse_stack[i];
 
+    INIT_DYNARRAY(copy-&gt;token_buffer, 0, 2);
     RESIZE_DYNARRAY(copy-&gt;token_buffer, orig-&gt;token_buffer_len);
     for(int i = 0; i &lt; orig-&gt;token_buffer_len; i++)
         copy-&gt;token_buffer[i] = orig-&gt;token_buffer[i];
@@ -789,7 +710,8 @@ void gzl_free_parse_state(struct gzl_parse_state *s)
     free(s);
 }
 
-void gzl_init_parse_state(struct gzl_parse_state *s, struct gzl_bound_grammar *bg)
+void gzl_init_parse_state(struct gzl_parse_state *s,
+                          struct gzl_bound_grammar *bg)
 {
     s-&gt;offset.byte = 0;
     s-&gt;offset.line = 1;
@@ -812,8 +734,8 @@ void gzl_init_parse_state(struct gzl_parse_state *s, struct gzl_bound_grammar *b
 }
 
 enum gzl_status gzl_parse_file(struct gzl_parse_state *state,
-                                     FILE *file, void *user_data,
-                                     int max_buffer_size)
+                               FILE *file, void *user_data,
+                               int max_buffer_size)
 {
     struct gzl_buffer *buffer = malloc(sizeof(*buffer));
     INIT_DYNARRAY(buffer-&gt;buf, 0, 4096);
@@ -830,35 +752,29 @@ enum gzl_status gzl_parse_file(struct gzl_parse_state *state,
 
     enum gzl_status status;
     bool is_eof = false;
-    do
-    {
+    do {
         /* Make sure we have space for at least min_new_data new data. */
         size_t new_buf_size = buffer-&gt;buf_size;
         while(buffer-&gt;buf_len + min_new_data &gt; new_buf_size)
           buffer-&gt;buf_size *= 2;
-        if(new_buf_size &gt; max_buffer_size)
-        {
+        if(new_buf_size &gt; max_buffer_size) {
             status = GZL_STATUS_RESOURCE_LIMIT_EXCEEDED;
             break;
         }
-        if(new_buf_size != buffer-&gt;buf_size)
-        {
+        if(new_buf_size != buffer-&gt;buf_size) {
             buffer-&gt;buf_size = new_buf_size;
             buffer-&gt;buf = realloc(buffer-&gt;buf, new_buf_size);
         }
         size_t bytes_to_read = buffer-&gt;buf_size - buffer-&gt;buf_len;
 
         /* Do the I/O and check for errors. */
-        size_t bytes_read = fread(buffer-&gt;buf + buffer-&gt;buf_len, 1, bytes_to_read, file);
-        if(bytes_read &lt; bytes_to_read)
-        {
-            if(ferror(file))
-            {
+        size_t bytes_read = fread(buffer-&gt;buf + buffer-&gt;buf_len, 1,
+                                  bytes_to_read, file);
+        if(bytes_read &lt; bytes_to_read) {
+            if(ferror(file)) {
                 status = GZL_STATUS_IO_ERROR;
                 break;
-            }
-            else if(feof(file))
-            {
+            } else if(feof(file)) {
                 is_eof = true;
             }
         }
@@ -897,28 +813,20 @@ enum gzl_status gzl_parse_file(struct gzl_parse_state *state,
         buffer-&gt;buf_len = bytes_to_save;
     } while(status == GZL_STATUS_OK &amp;&amp; !is_eof);
 
-    if(status == GZL_STATUS_HARD_EOF || (status == GZL_STATUS_OK &amp;&amp; is_eof))
-    {
-        if(gzl_finish_parse(state))
-        {
-            if(!feof(file) || buffer-&gt;buf_len &gt; 0)
-            {
+    if(status == GZL_STATUS_HARD_EOF || (status == GZL_STATUS_OK &amp;&amp; is_eof)) {
+        if(gzl_finish_parse(state)) {
+            if(!feof(file) || buffer-&gt;buf_len &gt; 0) {
                 /* There was data left over -- we hit grammar EOF before
                  * file EOF. */
                 status = GZL_STATUS_OK;
-            }
-            else
-            {
+            } else
                 status = GZL_STATUS_PREMATURE_EOF_ERROR;
-            }
-        }
-        else
+        } else
             status = GZL_STATUS_PREMATURE_EOF_ERROR;
     }
 
     FREE_DYNARRAY(buffer-&gt;buf);
     free(buffer);
-
     return status;
 }
 </diff>
      <filename>runtime/parse.c</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>818141dde1000b397e44568c6185da771976e2ae</id>
    </parent>
  </parents>
  <author>
    <name>Joshua Haberman</name>
    <email>joshua@reverberate.org</email>
  </author>
  <url>http://github.com/haberman/gazelle/commit/338a003e541f73d9c22741174362447f56654d02</url>
  <id>338a003e541f73d9c22741174362447f56654d02</id>
  <committed-date>2009-02-21T01:15:53-08:00</committed-date>
  <authored-date>2009-02-21T01:15:53-08:00</authored-date>
  <message>Respect 80 chars and terseness in C parsing code.</message>
  <tree>79121595b4d3c3ec73f3553c143b096c64444ba5</tree>
  <committer>
    <name>Joshua Haberman</name>
    <email>joshua@reverberate.org</email>
  </committer>
</commit>
