mirror of
https://github.com/janet-lang/janet
synced 2024-06-16 10:19:55 +00:00
Add (number combinator to peg).
This allows using Janet's number parser without creating intermediate strings.
This commit is contained in:
parent
cea14a6869
commit
cccbdc164c
|
@ -1,6 +1,10 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## Unreleased - ???
|
||||||
|
- Add `(number ...)` pattern to peg for more efficient number parsing using Janet's
|
||||||
|
scan-number function without immediate string creation.
|
||||||
|
|
||||||
## 1.17.2 - 2021-09-18
|
## 1.17.2 - 2021-09-18
|
||||||
- Remove include of windows.h from janet.h. This caused issues on certain projects.
|
- Remove include of windows.h from janet.h. This caused issues on certain projects.
|
||||||
- Fix formatting in doc-format to better handle special characters in signatures.
|
- Fix formatting in doc-format to better handle special characters in signatures.
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -159,7 +159,7 @@ build/c/janet.c: build/janet_boot src/boot/boot.janet
|
||||||
##### Amalgamation #####
|
##### Amalgamation #####
|
||||||
########################
|
########################
|
||||||
|
|
||||||
SONAME=libjanet.so.1.17
|
SONAME=libjanet.so.1.18
|
||||||
|
|
||||||
build/c/shell.c: src/mainclient/shell.c
|
build/c/shell.c: src/mainclient/shell.c
|
||||||
cp $< $@
|
cp $< $@
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
project('janet', 'c',
|
project('janet', 'c',
|
||||||
default_options : ['c_std=c99', 'build.c_std=c99', 'b_lundef=false', 'default_library=both'],
|
default_options : ['c_std=c99', 'build.c_std=c99', 'b_lundef=false', 'default_library=both'],
|
||||||
version : '1.17.2')
|
version : '1.18.0')
|
||||||
|
|
||||||
# Global settings
|
# Global settings
|
||||||
janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet')
|
janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet')
|
||||||
|
|
|
@ -4,10 +4,10 @@
|
||||||
#define JANETCONF_H
|
#define JANETCONF_H
|
||||||
|
|
||||||
#define JANET_VERSION_MAJOR 1
|
#define JANET_VERSION_MAJOR 1
|
||||||
#define JANET_VERSION_MINOR 17
|
#define JANET_VERSION_MINOR 18
|
||||||
#define JANET_VERSION_PATCH 2
|
#define JANET_VERSION_PATCH 0
|
||||||
#define JANET_VERSION_EXTRA ""
|
#define JANET_VERSION_EXTRA "-dev"
|
||||||
#define JANET_VERSION "1.17.2"
|
#define JANET_VERSION "1.18.0-dev"
|
||||||
|
|
||||||
/* #define JANET_BUILD "local" */
|
/* #define JANET_BUILD "local" */
|
||||||
|
|
||||||
|
|
|
@ -387,6 +387,24 @@ tail:
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case RULE_CAPTURE_NUM: {
|
||||||
|
down1(s);
|
||||||
|
const uint8_t *result = peg_rule(s, s->bytecode + rule[1], text);
|
||||||
|
up1(s);
|
||||||
|
if (!result) return NULL;
|
||||||
|
/* check number parsing */
|
||||||
|
double x = 0.0;
|
||||||
|
if (janet_scan_number(text, (int32_t)(result - text), &x)) return NULL;
|
||||||
|
/* Specialized pushcap - avoid intermediate string creation */
|
||||||
|
if (!s->has_backref && s->mode == PEG_MODE_ACCUMULATE) {
|
||||||
|
janet_buffer_push_bytes(s->scratch, text, (int32_t)(result - text));
|
||||||
|
} else {
|
||||||
|
uint32_t tag = rule[2];
|
||||||
|
pushcap(s, janet_wrap_number(x), tag);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
case RULE_ACCUMULATE: {
|
case RULE_ACCUMULATE: {
|
||||||
uint32_t tag = rule[2];
|
uint32_t tag = rule[2];
|
||||||
int oldmode = s->mode;
|
int oldmode = s->mode;
|
||||||
|
@ -965,6 +983,9 @@ static void spec_cap1(Builder *b, int32_t argc, const Janet *argv, uint32_t op)
|
||||||
static void spec_capture(Builder *b, int32_t argc, const Janet *argv) {
|
static void spec_capture(Builder *b, int32_t argc, const Janet *argv) {
|
||||||
spec_cap1(b, argc, argv, RULE_CAPTURE);
|
spec_cap1(b, argc, argv, RULE_CAPTURE);
|
||||||
}
|
}
|
||||||
|
static void spec_capture_number(Builder *b, int32_t argc, const Janet *argv) {
|
||||||
|
spec_cap1(b, argc, argv, RULE_CAPTURE_NUM);
|
||||||
|
}
|
||||||
static void spec_accumulate(Builder *b, int32_t argc, const Janet *argv) {
|
static void spec_accumulate(Builder *b, int32_t argc, const Janet *argv) {
|
||||||
spec_cap1(b, argc, argv, RULE_ACCUMULATE);
|
spec_cap1(b, argc, argv, RULE_ACCUMULATE);
|
||||||
}
|
}
|
||||||
|
@ -1118,6 +1139,7 @@ static const SpecialPair peg_specials[] = {
|
||||||
{"line", spec_line},
|
{"line", spec_line},
|
||||||
{"look", spec_look},
|
{"look", spec_look},
|
||||||
{"not", spec_not},
|
{"not", spec_not},
|
||||||
|
{"number", spec_capture_number},
|
||||||
{"opt", spec_opt},
|
{"opt", spec_opt},
|
||||||
{"position", spec_position},
|
{"position", spec_position},
|
||||||
{"quote", spec_capture},
|
{"quote", spec_capture},
|
||||||
|
@ -1422,6 +1444,7 @@ static void *peg_unmarshal(JanetMarshalContext *ctx) {
|
||||||
case RULE_ACCUMULATE:
|
case RULE_ACCUMULATE:
|
||||||
case RULE_GROUP:
|
case RULE_GROUP:
|
||||||
case RULE_CAPTURE:
|
case RULE_CAPTURE:
|
||||||
|
case RULE_CAPTURE_NUM:
|
||||||
case RULE_UNREF:
|
case RULE_UNREF:
|
||||||
/* [rule, tag] */
|
/* [rule, tag] */
|
||||||
if (rule[1] >= blen) goto bad;
|
if (rule[1] >= blen) goto bad;
|
||||||
|
|
|
@ -246,7 +246,7 @@ static double convert(
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Scan a real (double) from a string. If the string cannot be converted into
|
/* Scan a real (double) from a string. If the string cannot be converted into
|
||||||
* and integer, set *err to 1 and return 0. */
|
* and integer, return 0. */
|
||||||
int janet_scan_number(
|
int janet_scan_number(
|
||||||
const uint8_t *str,
|
const uint8_t *str,
|
||||||
int32_t len,
|
int32_t len,
|
||||||
|
|
|
@ -2040,7 +2040,8 @@ typedef enum {
|
||||||
RULE_READINT, /* [(signedness << 4) | (endianess << 5) | bytewidth, tag] */
|
RULE_READINT, /* [(signedness << 4) | (endianess << 5) | bytewidth, tag] */
|
||||||
RULE_LINE, /* [tag] */
|
RULE_LINE, /* [tag] */
|
||||||
RULE_COLUMN, /* [tag] */
|
RULE_COLUMN, /* [tag] */
|
||||||
RULE_UNREF /* [rule, tag] */
|
RULE_UNREF, /* [rule, tag] */
|
||||||
|
RULE_CAPTURE_NUM /* [rule, tag] */
|
||||||
} JanetPegOpcod;
|
} JanetPegOpcod;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
@ -344,4 +344,8 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
|
||||||
(assert (deep= @[] (peg/match '(* "test" (any 1)) @"test")) "peg empty pattern 5")
|
(assert (deep= @[] (peg/match '(* "test" (any 1)) @"test")) "peg empty pattern 5")
|
||||||
(assert (deep= @[] (peg/match '(* "test" (any 1)) (buffer "test"))) "peg empty pattern 6")
|
(assert (deep= @[] (peg/match '(* "test" (any 1)) (buffer "test"))) "peg empty pattern 6")
|
||||||
|
|
||||||
|
# number pattern
|
||||||
|
(assert (deep= @[111] (peg/match '(number :d+) "111")) "simple number capture 1")
|
||||||
|
(assert (deep= @[255] (peg/match '(number :w+) "0xff")) "simple number capture 2")
|
||||||
|
|
||||||
(end-suite)
|
(end-suite)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user