Skip to content

Commit 4b6c433

Browse files
authored
Merge pull request #1077 from CraftSpider/bibtex-rustify-exec
Rustify engine_bibtex - part 3, bibstyle interpreter
2 parents cc83b75 + c2f1693 commit 4b6c433

22 files changed

Lines changed: 2722 additions & 2989 deletions

File tree

crates/bridge_harfbuzz/harfbuzz

Submodule harfbuzz updated 238 files

crates/engine_bibtex/bibtex/bibtex.c

Lines changed: 38 additions & 1765 deletions
Large diffs are not rendered by default.

crates/engine_bibtex/bibtex/bibtex_bindings.h

Lines changed: 22 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#define HASH_BASE 1
1717

18+
19+
1820
typedef enum {
1921
BUF_TY_BASE,
2022
BUF_TY_SV,
@@ -70,33 +72,25 @@ typedef enum {
7072
SCAN_RES_WHITESPACE_ADJACENT = 3,
7173
} ScanRes;
7274

73-
typedef enum {
74-
STK_TYPE_INTEGER = 0,
75-
STK_TYPE_STRING = 1,
76-
STK_TYPE_FUNCTION = 2,
77-
STK_TYPE_MISSING = 3,
78-
STK_TYPE_ILLEGAL = 4,
79-
} StkType;
80-
8175
enum StrIlk
8276
#ifdef __cplusplus
8377
: uint8_t
8478
#endif // __cplusplus
8579
{
86-
StrIlk_Text = 0,
87-
StrIlk_Integer = 1,
88-
StrIlk_AuxCommand = 2,
89-
StrIlk_AuxFile = 3,
90-
StrIlk_BstCommand = 4,
91-
StrIlk_BstFile = 5,
92-
StrIlk_BibFile = 6,
93-
StrIlk_FileExt = 7,
94-
StrIlk_Cite = 9,
95-
StrIlk_LcCite = 10,
96-
StrIlk_BstFn = 11,
97-
StrIlk_BibCommand = 12,
98-
StrIlk_Macro = 13,
99-
StrIlk_ControlSeq = 14,
80+
STR_ILK_TEXT = 0,
81+
STR_ILK_INTEGER = 1,
82+
STR_ILK_AUX_COMMAND = 2,
83+
STR_ILK_AUX_FILE = 3,
84+
STR_ILK_BST_COMMAND = 4,
85+
STR_ILK_BST_FILE = 5,
86+
STR_ILK_BIB_FILE = 6,
87+
STR_ILK_FILE_EXT = 7,
88+
STR_ILK_CITE = 9,
89+
STR_ILK_LC_CITE = 10,
90+
STR_ILK_BST_FN = 11,
91+
STR_ILK_BIB_COMMAND = 12,
92+
STR_ILK_MACRO = 13,
93+
STR_ILK_CONTROL_SEQ = 14,
10094
};
10195
#ifndef __cplusplus
10296
typedef uint8_t StrIlk;
@@ -170,36 +164,27 @@ typedef uintptr_t BibNumber;
170164

171165
typedef uintptr_t CiteNumber;
172166

173-
typedef uintptr_t HashPointer2;
174-
175167
typedef struct {
176168
CiteNumber cite_loc;
177169
CiteNumber lc_cite_loc;
178170
bool cite_found;
179171
bool lc_found;
180172
} FindCiteLocs;
181173

182-
typedef struct {
183-
StkType typ;
184-
int32_t lit;
185-
} ExecVal;
186-
187174
typedef struct {
188175
Bibtex *glbl_ctx;
189176
HashPointer _default;
190-
ExecVal pop1;
191-
ExecVal pop2;
192-
ExecVal pop3;
193177
XBuf_ExecVal *lit_stack;
194178
uintptr_t lit_stk_ptr;
195179
bool mess_with_entries;
180+
/**
181+
* Pointer to the current top of the string pool, used to optimized certain string operations
182+
*/
196183
StrNumber bib_str_ptr;
197184
} ExecCtx;
198185

199186
typedef uintptr_t FieldLoc;
200187

201-
typedef uintptr_t WizFnLoc;
202-
203188
typedef uintptr_t PoolPointer;
204189

205190
typedef struct {
@@ -251,8 +236,6 @@ extern "C" {
251236

252237
extern const LexClass LEX_CLASS[256];
253238

254-
extern const int32_t CHAR_WIDTH[256];
255-
256239
void reset_all(void);
257240

258241
bool bib_str_eq_buf(StrNumber s, BufTy buf, BufPointer ptr, BufPointer len);
@@ -293,12 +276,8 @@ void set_bib_ptr(BibNumber num);
293276

294277
void check_bib_files(BibNumber ptr);
295278

296-
StrNumber cur_preamble(void);
297-
298279
BibNumber preamble_ptr(void);
299280

300-
void set_preamble_ptr(BibNumber num);
301-
302281
int32_t bib_line_num(void);
303282

304283
void set_bib_line_num(int32_t num);
@@ -319,16 +298,8 @@ BufPointer bib_buf_len(BufTy ty);
319298

320299
void bib_set_buf_len(BufTy ty, BufPointer len);
321300

322-
BufPointer name_tok(BufPointer pos);
323-
324-
void set_name_tok(BufPointer pos, BufPointer val);
325-
326301
void lower_case(BufTy buf, BufPointer ptr, BufPointer len);
327302

328-
void upper_case(BufTy buf, BufPointer ptr, BufPointer len);
329-
330-
BufPointer int_to_ascii(int32_t the_int, BufTy int_buf, BufPointer int_begin);
331-
332303
void quick_sort(CiteNumber left_end, CiteNumber right_end);
333304

334305
StrNumber cite_list(CiteNumber num);
@@ -347,9 +318,9 @@ StrNumber cite_info(CiteNumber num);
347318

348319
void set_cite_info(CiteNumber num, StrNumber info);
349320

350-
HashPointer2 type_list(CiteNumber num);
321+
HashPointer type_list(CiteNumber num);
351322

352-
void set_type_list(CiteNumber num, HashPointer2 ty);
323+
void set_type_list(CiteNumber num, HashPointer ty);
353324

354325
bool entry_exists(CiteNumber num);
355326

@@ -387,81 +358,22 @@ uintptr_t num_ent_strs(void);
387358

388359
void set_num_ent_strs(uintptr_t val);
389360

390-
int32_t entry_ints(int32_t pos);
391-
392-
void set_entry_ints(int32_t pos, int32_t val);
393-
394-
ASCIICode entry_strs(int32_t pos);
395-
396-
void set_entry_strs(int32_t pos, ASCIICode val);
397-
398-
ExecVal int_val(int32_t lit);
399-
400-
ExecVal str_val(StrNumber str);
401-
402-
ExecVal func_val(HashPointer f);
403-
404-
ExecVal missing_val(StrNumber f);
405-
406361
ExecCtx init_exec_ctx(Bibtex *glbl_ctx);
407362

408-
CResult print_lit(ExecVal val);
409-
410-
CResult print_stk_lit(ExecVal val);
411-
412-
CResult print_wrong_stk_lit(ExecCtx *ctx, ExecVal val, StkType typ2);
413-
414-
CResult bst_ex_warn_print(const ExecCtx *ctx);
415-
416363
CResult print_bst_name(const Bibtex *glbl_ctx);
417364

418-
void push_lit_stk(ExecCtx *ctx, ExecVal val);
419-
420-
CResult pop_lit_stk(ExecCtx *ctx, ExecVal *out);
421-
422-
CResult pop_top_and_print(ExecCtx *ctx);
423-
424-
CResult pop_whole_stack(ExecCtx *ctx);
425-
426365
void init_command_execution(ExecCtx *ctx);
427366

428-
CResult figure_out_the_formatted_name(ExecCtx *ctx,
429-
BufPointer first_start,
430-
BufPointer first_end,
431-
BufPointer last_end,
432-
BufPointer von_start,
433-
BufPointer von_end,
434-
BufPointer *name_bf_ptr,
435-
BufPointer *name_bf_xptr,
436-
BufPointer jr_end,
437-
int32_t *brace_level);
438-
439367
CResult check_command_execution(ExecCtx *ctx);
440368

441-
CResult add_pool_buf_and_push(ExecCtx *ctx);
442-
443-
ExecVal *cur_lit(ExecCtx *ctx);
369+
CResult execute_fn(ExecCtx *ctx, HashPointer ex_fn_loc);
444370

445371
int32_t num_glb_strs(void);
446372

447373
void set_num_glb_strs(int32_t val);
448374

449375
void check_grow_global_strs(void);
450376

451-
uintptr_t glb_bib_str_ptr(uintptr_t pos);
452-
453-
void set_glb_bib_str_ptr(uintptr_t pos, uintptr_t val);
454-
455-
ASCIICode global_strs(uintptr_t pos);
456-
457-
void set_global_strs(uintptr_t pos, ASCIICode val);
458-
459-
int32_t glb_str_end(uintptr_t pos);
460-
461-
void set_glb_str_end(uintptr_t pos, int32_t val);
462-
463-
uintptr_t end_of_def(void);
464-
465377
uintptr_t undefined(void);
466378

467379
FnClass fn_type(HashPointer pos);
@@ -532,8 +444,6 @@ CResult bst_warn_print(const Bibtex *ctx);
532444

533445
void eat_bst_print(void);
534446

535-
void unknwn_function_class_confusion(void);
536-
537447
CResult bst_id_print(ScanRes scan_result);
538448

539449
void bst_left_brace_print(void);
@@ -560,14 +470,6 @@ CResult bad_cross_reference_print(StrNumber s);
560470

561471
CResult print_missing_entry(StrNumber s);
562472

563-
CResult bst_cant_mess_with_entries_print(const ExecCtx *ctx);
564-
565-
void bst_1print_string_size_exceeded(void);
566-
567-
CResult bst_2print_string_size_exceeded(const ExecCtx *ctx);
568-
569-
void case_conversion_confusion(void);
570-
571473
void print_fn_class(HashPointer fn_loc);
572474

573475
CResult bst_err_print_and_look_for_blank_line(Bibtex *ctx);
@@ -576,10 +478,6 @@ CResult already_seen_function_print(Bibtex *ctx, HashPointer seen_fn_loc);
576478

577479
CResult nonexistent_cross_reference_error(FieldLoc field_ptr);
578480

579-
void output_bbl_line(Bibtex *ctx);
580-
581-
HashPointer2 wiz_functions(WizFnLoc pos);
582-
583481
StrNumber field_info(FieldLoc pos);
584482

585483
void set_field_info(FieldLoc pos, StrNumber val);
@@ -604,14 +502,8 @@ bool tectonic_eof(PeekableInput *peekable);
604502

605503
bool input_ln(PeekableInput *peekable);
606504

607-
bool bib_str_eq_str(StrNumber s1, StrNumber s2);
608-
609-
void pool_overflow(void);
610-
611505
ASCIICode bib_str_pool(PoolPointer idx);
612506

613-
void bib_set_str_pool(PoolPointer idx, ASCIICode code);
614-
615507
StrNumber bib_str_ptr(void);
616508

617509
void bib_set_str_ptr(StrNumber ptr);
@@ -620,24 +512,14 @@ PoolPointer bib_str_start(StrNumber s);
620512

621513
void bib_set_str_start(StrNumber s, PoolPointer ptr);
622514

623-
uintptr_t bib_pool_size(void);
624-
625515
uintptr_t bib_max_strings(void);
626516

627-
PoolPointer bib_pool_ptr(void);
628-
629517
void bib_set_pool_ptr(PoolPointer ptr);
630518

631-
CResultStr bib_make_string(void);
632-
633-
void add_buf_pool(StrNumber p_str);
634-
635519
CResultLookup str_lookup(BufTy buf, BufPointer ptr, BufPointer len, StrIlk ilk, bool insert);
636520

637521
CResult pre_def_certain_strings(Bibtex *ctx);
638522

639-
void add_out_pool(Bibtex *ctx, StrNumber str);
640-
641523
bool scan1(ASCIICode char1);
642524

643525
bool scan1_white(ASCIICode char1);
@@ -661,20 +543,6 @@ CResultBool scan_and_store_the_field_value_and_eat_white(Bibtex *ctx,
661543
ASCIICode right_outer_delim,
662544
HashPointer field_name_loc);
663545

664-
CResult decr_brace_level(const ExecCtx *ctx, StrNumber pop_lit_var, int32_t *brace_level);
665-
666-
CResult check_brace_level(const ExecCtx *ctx, StrNumber pop_lit_var, int32_t brace_level);
667-
668-
CResult name_scan_for_and(ExecCtx *ctx, StrNumber pop_lit_var, int32_t *brace_level);
669-
670-
CResultBool von_token_found(BufPointer *name_bf_ptr, BufPointer name_bf_xptr);
671-
672-
CResult von_name_ends_and_last_name_starts_stuff(BufPointer last_end,
673-
BufPointer von_start,
674-
BufPointer *von_end,
675-
BufPointer *name_bf_ptr,
676-
BufPointer *name_bf_xptr);
677-
678546
#ifdef __cplusplus
679547
} // extern "C"
680548
#endif // __cplusplus

crates/engine_bibtex/src/c_api/bibs.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
char_info::LexClass,
55
log::{rs_eat_bib_print, write_log_file},
66
peekable::{rs_input_ln, PeekableInput},
7+
pool::StringPool,
78
scan::Scan,
89
xbuf::XBuf,
910
BibNumber, StrNumber,
@@ -72,6 +73,18 @@ impl BibData {
7273
self.preamble_ptr += 1;
7374
}
7475

76+
pub fn preamble_ptr(&self) -> usize {
77+
self.preamble_ptr
78+
}
79+
80+
pub fn set_preamble_ptr(&mut self, val: usize) {
81+
self.preamble_ptr = val;
82+
}
83+
84+
pub fn cur_preamble(&self) -> StrNumber {
85+
self.preamble[self.preamble_ptr]
86+
}
87+
7588
fn grow(&mut self) {
7689
self.bib_list.grow(MAX_BIB_FILES);
7790
self.bib_file.grow(MAX_BIB_FILES);
@@ -134,21 +147,11 @@ pub extern "C" fn check_bib_files(ptr: BibNumber) {
134147
})
135148
}
136149

137-
#[no_mangle]
138-
pub extern "C" fn cur_preamble() -> StrNumber {
139-
with_bibs(|bibs| bibs.preamble[bibs.preamble_ptr])
140-
}
141-
142150
#[no_mangle]
143151
pub extern "C" fn preamble_ptr() -> BibNumber {
144152
with_bibs(|bibs| bibs.preamble_ptr)
145153
}
146154

147-
#[no_mangle]
148-
pub extern "C" fn set_preamble_ptr(num: BibNumber) {
149-
with_bibs_mut(|bibs| bibs.preamble_ptr = num)
150-
}
151-
152155
#[no_mangle]
153156
pub extern "C" fn bib_line_num() -> i32 {
154157
with_bibs(|bibs| bibs.bib_line_num)
@@ -190,6 +193,7 @@ pub extern "C" fn eat_bib_white_space() -> bool {
190193

191194
pub fn compress_bib_white(
192195
buffers: &mut GlobalBuffer,
196+
pool: &StringPool,
193197
at_bib_command: bool,
194198
) -> Result<bool, BibtexError> {
195199
if buffers.offset(BufTy::Ex, 1) == buffers.len() {
@@ -210,7 +214,7 @@ pub fn compress_bib_white(
210214
});
211215

212216
if res {
213-
return rs_eat_bib_print(buffers, at_bib_command).map(|_| false);
217+
return rs_eat_bib_print(buffers, pool, at_bib_command).map(|_| false);
214218
}
215219

216220
with_bibs_mut(|bibs| {

0 commit comments

Comments
 (0)