Skip to content

Commit 5e9b14a

Browse files
committed
FEAT: implemented transcode/line for getting information about number of lines scanned
related to: Oldes/Rebol-issues#2302
1 parent 047f032 commit 5e9b14a

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/boot/natives.reb

+2
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ transcode: native [
658658
/one "Translate next complete value (returns the value only)"
659659
/only "Translate only a single value (blocks dissected)"
660660
/error "Do not cause errors - return error object as value in place"
661+
/line "Return also information about number of lines scaned"
662+
count [integer!] "Initial line number"
661663
]
662664

663665
echo: native [

src/core/l-scan.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,8 @@ extern REBSER *Scan_Full_Block(SCAN_STATE *scan_state, REBYTE mode_char);
18961896
REBOOL one = D_REF(3);
18971897
REBOOL only = D_REF(4);
18981898
REBOOL relax = D_REF(5);
1899+
REBOOL line = D_REF(6);
1900+
REBVAL *count = D_ARG(7);
18991901
REBSER *blk;
19001902
REBSER *ser;
19011903
REBYTE *bin;
@@ -1917,7 +1919,10 @@ extern REBSER *Scan_Full_Block(SCAN_STATE *scan_state, REBYTE mode_char);
19171919
if (next || one) SET_FLAG(scan_state.opts, SCAN_NEXT);
19181920
if (only) SET_FLAG(scan_state.opts, SCAN_ONLY);
19191921
if (relax) SET_FLAG(scan_state.opts, SCAN_RELAX);
1920-
1922+
if (line) {
1923+
if (0 >= VAL_INT64(count)) Trap1(RE_OUT_OF_RANGE, count);
1924+
scan_state.line_count = VAL_UNT32(count);
1925+
}
19211926
// Scan_Code clears the next flag!
19221927
// Decide if result should contain also modified input position.
19231928
// (with refinements /next, /only and /error)
@@ -1957,6 +1962,10 @@ extern REBSER *Scan_Full_Block(SCAN_STATE *scan_state, REBYTE mode_char);
19571962
VAL_INDEX(src) = len;
19581963
}
19591964
Append_Val(blk, src);
1965+
if (line) {
1966+
SET_INTEGER(count, scan_state.line_count);
1967+
Append_Val(blk, count);
1968+
}
19601969
}
19611970
return R_RET;
19621971
}

src/tests/units/lexer-test.r3

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ Rebol [
3535
--assert 1 = transcode/one "1 2"
3636
--assert [1 2] = transcode/one "[1 2]"
3737

38+
--test-- "transcode/line"
39+
--assert all [error? e: try [transcode "1 1d"] e/near = "(line 1) 1 1d"]
40+
--assert all [error? e: try [transcode "1^/1d"] e/near = "(line 2) 1d"]
41+
--assert all [error? e: try [transcode/line "1 1d" 10] e/near = "(line 10) 1 1d"]
42+
--assert all [error? e: try [transcode/line "1^/1d" 10] e/near = "(line 11) 1d"]
43+
--assert all [
44+
code: "1^/2" line: 1
45+
set [value code line] transcode/next/line :code :line
46+
value = 1 line = 1
47+
set [value code line] transcode/next/line :code :line
48+
value = 2 line = 2
49+
]
50+
3851
===end-group===
3952

4053
===start-group=== "Invalid construction"

0 commit comments

Comments
 (0)