]> ocean-lang.org Git - ocean/commitdiff
parsergen: move "Memory allocation" section.
authorNeilBrown <neil@brown.name>
Fri, 5 Mar 2021 09:16:22 +0000 (20:16 +1100)
committerNeilBrown <neil@brown.name>
Wed, 10 Mar 2021 01:00:31 +0000 (12:00 +1100)
This movement isn't really significant yet, but it will help a bit
later, and I want to get it out of the way.

Signed-off-by: NeilBrown <neil@brown.name>
csrc/parsergen.mdc

index f2bbe0151d75977dd2b2ae9f16031f4c171a5949..36c7464d58703859c2b59ab881ab7d595d3a0242 100644 (file)
@@ -2662,6 +2662,35 @@ table.
                        return -1;
        }
 
+### Memory allocation
+
+The `scanner` returns tokens in a local variable - we want them in allocated
+memory so they can live in the `asn_stack`.  Similarly the `asn` produced by
+a reduce is in a large buffer.  Both of these require some allocation and
+copying, hence `memdup` and `tok_copy`.
+
+###### parser includes
+       #include <memory.h>
+
+###### parser functions
+
+       void *memdup(void *m, int len)
+       {
+               void *ret;
+               if (len == 0)
+                       return NULL;
+               ret = malloc(len);
+               memcpy(ret, m, len);
+               return ret;
+       }
+
+       static struct token *tok_copy(struct token tk)
+       {
+               struct token *new = malloc(sizeof(*new));
+               *new = tk;
+               return new;
+       }
+
 ### The state stack.
 
 The core data structure for the parser is the stack.  This tracks all
@@ -2834,35 +2863,6 @@ before we `shift` the nonterminal in.
                return indents;
        }
 
-### Memory allocation
-
-The `scanner` returns tokens in a local variable - we want them in allocated
-memory so they can live in the `asn_stack`.  Similarly the `asn` produced by
-a reduce is in a large buffer.  Both of these require some allocation and
-copying, hence `memdup` and `tokcopy`.
-
-###### parser includes
-       #include <memory.h>
-
-###### parser functions
-
-       void *memdup(void *m, int len)
-       {
-               void *ret;
-               if (len == 0)
-                       return NULL;
-               ret = malloc(len);
-               memcpy(ret, m, len);
-               return ret;
-       }
-
-       static struct token *tok_copy(struct token tk)
-       {
-               struct token *new = malloc(sizeof(*new));
-               *new = tk;
-               return new;
-       }
-
 ### The heart of the parser.
 
 Now we have the parser.  For each token we might shift it, trigger a