Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequester Hyper StateNode #39

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/hyper/htadd.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2010, Gabriel Dos Reis.
Copyright (C) 2007-2023, Gabriel Dos Reis.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -320,9 +320,11 @@ update_db(FILE *db, FILE *temp_db, FILE *new_file,
get_token();
mtime = atoi(token.id);
if (strcmp(fname, addname) == 0) {
save_scanner_state();
add_new_pages(temp_db, new_file, addname, fullname);
restore_scanner_state();
// Temporarily save IO state, add the new pages, and restore.
{
OpenAxiom::IOStateManager save_io_state { };
add_new_pages(temp_db, new_file, addname, fullname);
}
file_there = 1;
while ((c = get_char()) != EOF) {
if (c == '\t')
Expand Down
91 changes: 54 additions & 37 deletions src/hyper/lex.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2008, Gabriel Dos Reis.
Copyright (C) 2007-2023, Gabriel Dos Reis.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -34,21 +34,25 @@
*/

/*
* Lexical analyzer stuff. Exported functions: parser_init() --
* initialize the parser tables with keywords init_scanner() --
* initialize scanner for reading a new page get_token() --
* sets the "token" variable to be the next -- token in the current input
* stream save_scanner_state( ) -- save the current state of scanner so
* that -- the scanner input mode may be switched restore_scanner_state() --
* undo the saved state
* Lexical analyzer stuff. Exported functions:
* -- parser_init():
* initialize the parser tables with keywords
* -- init_scanner():
* initialize scanner for reading a new page
* -- get_token():
* sets the "token" variable to be the next token in the current input
* stream
* -- OpenAxiom::IOStateManager:
* save the current state of scanner so that the scanner input mode may
* be switched and then undo the saved state
*
* Note: The scanner reads from three seperate input locations depending on the
* value of the variable "input_type". If this variable is:
*
* FromFile -- it read from the file pointed to by "cfile". FromString
* -- It reads from the string "input_string". FromSpadSocket -- It reads
* from the socket pointed to by spad_socket FromFD -- It reads from a
* file descriptor
* -- FromFile: it read from the file pointed to by "cfile".
* -- FromString: It reads from the string "input_string".
* -- FromSpadSocket: It reads from the socket pointed to by spad_socket.
* -- FromFD: It reads from a file descriptor.
*
*/
#define _LEX_C
Expand All @@ -57,6 +61,8 @@
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stack>
#include <vector>

#include "debug.h"
#include "sockio.h"
Expand All @@ -75,9 +81,25 @@ static int keyword_type(void );
extern int gTtFontIs850;
extern HDWindow *gWindow;

/** I am implementing a state node stack, this is the structure I store **/

struct IOState {
int last_ch, last_token;
int line_number;
SourceInputKind input_type;
long fpos, keyword_fpos;
long page_start_fpos;
Token token;
char *input_string;
FILE *cfile;
int keyword;
};

using IOStateStack = std::stack<IOState, std::vector<IOState>>;

/** The stack of IOStates. **/
static IOStateStack io_states;

StateNode *top_state_node;
HyperDocPage *gPageBeingParsed; /* page currently being parsed */
char ebuffer[128];
short int gInSpadsrc = 0;
Expand Down Expand Up @@ -258,50 +280,45 @@ init_scanner()
*/

/* save the current state of the scanner */
void
save_scanner_state()
OpenAxiom::IOStateManager::IOStateManager()
{
StateNode *new_item = (StateNode *) halloc((sizeof(StateNode)), "StateNode");

io_states.push({});
auto new_item = &io_states.top();
new_item->page_start_fpos = page_start_fpos;
new_item->fpos = fpos;
new_item->keyword_fpos = keyword_fpos;
new_item->line_number = line_number;
new_item->last_ch = last_ch;
new_item->last_token = last_token;
new_item->token = token;
new_item->input_type = input_type;
new_item->input_string = input_string;
new_item->cfile = cfile;
new_item->next = top_state_node;
new_item->keyword = keyword;
top_state_node = new_item;
}

/* restore the saved scanner state */
void
restore_scanner_state()
OpenAxiom::IOStateManager::~IOStateManager()
{
StateNode *x = top_state_node;

if (top_state_node == NULL) {
if (io_states.empty()) {
fprintf(stderr, "Restore Scanner State: State empty\n");
exit(-1);
}
top_state_node = top_state_node->next;
page_start_fpos = x->page_start_fpos;
fpos = x->fpos;
keyword_fpos = x->keyword_fpos;
last_ch = x->last_ch;
last_token = x->last_token;
token = x->token;
input_type = x->input_type;
input_string = x->input_string;
cfile = x->cfile;
keyword = x->keyword;
auto x = io_states.top();
io_states.pop();
page_start_fpos = x.page_start_fpos;
fpos = x.fpos;
keyword_fpos = x.keyword_fpos;
line_number = x.line_number;
last_ch = x.last_ch;
last_token = x.last_token;
token = x.token;
input_type = x.input_type;
input_string = x.input_string;
cfile = x.cfile;
keyword = x.keyword;
if (cfile != NULL)
fseek(cfile, fpos + page_start_fpos, 0);
/** Once that is done, lets throw away some memory **/
free(x);
}

/* return the character to the input stream. */
Expand Down
14 changes: 11 additions & 3 deletions src/hyper/lex.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2008, Gabriel Dos Reis.
Copyright (C) 2007-2023, Gabriel Dos Reis.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -42,11 +42,19 @@
#define KEYTYPE 2 /* unrecognized keyword found in lex.c */
#define Numerrors 2

namespace OpenAxiom {
// Type of IO state stack. Used to automate save/restore of IO states.
struct IOStateManager {
IOStateManager();
IOStateManager(const IOStateManager&) = delete;
IOStateManager(IOStateManager&&) = delete;
~IOStateManager();
};
}

extern void get_expected_token(int);
extern void parser_init();
extern void init_scanner();
extern void save_scanner_state();
extern void restore_scanner_state();
extern void unget_char(int);
extern int get_char();
extern void unget_token();
Expand Down
6 changes: 2 additions & 4 deletions src/hyper/macro.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2010, Gabriel Dos Reis.
Copyright (C) 2007-2023, Gabriel Dos Reis.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -108,11 +108,10 @@ load_macro(MacroStore *macro)
int size = 0;
char *trace;
char *macro_buff;
OpenAxiom::IOStateManager save_io_state { };

save_scanner_state();
cfile = find_fp(macro->fpos);


init_scanner();

/** First thing I should do is make sure that the name is correct ***/
Expand Down Expand Up @@ -167,7 +166,6 @@ load_macro(MacroStore *macro)
*trace++ = getc(cfile);
*trace = '\0';
macro->loaded = 1;
restore_scanner_state();
return macro_buff;
}

Expand Down
22 changes: 1 addition & 21 deletions src/hyper/node.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2008, Gabriel Dos Reis.
Copyright (C) 2007-2023, Gabriel Dos Reis.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -77,26 +77,6 @@ struct PasteNode;
#define Footer 3
#define Title 4



/** I am implementing a state node stack, this is the structure I store **/

struct StateNode {
int last_ch, last_token;
SourceInputKind input_type;
long fpos, keyword_fpos;
long page_start_fpos;
Token token;
char *input_string;
FILE *cfile;
int keyword;
StateNode *next;
};

/** pointer to the top of the state node graph **/
extern StateNode *top_state_node;


/* structure for a hyper text link */
struct HyperLink {
int type; /* Memolink, Spadlink, Downlink etc. */
Expand Down
6 changes: 2 additions & 4 deletions src/hyper/parse-paste.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2010, Gabriel Dos Reis.
Copyright (C) 2007-2023, Gabriel Dos Reis.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -349,9 +349,8 @@ load_patch(PatchStore *patch)
int size = 0;
int limsize;
char *trace;
OpenAxiom::IOStateManager save_io_state { };


save_scanner_state();
cfile = find_fp(patch->fpos);

init_scanner();
Expand All @@ -377,7 +376,6 @@ load_patch(PatchStore *patch)
*trace++ = getc(cfile);
*trace = '\0';
patch->loaded = 1;
restore_scanner_state();
}


5 changes: 2 additions & 3 deletions src/hyper/parse.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2016, Gabriel Dos Reis.
Copyright (C) 2007-2023, Gabriel Dos Reis.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -230,13 +230,12 @@ format_page(UnloadedPage *ulpage)
void
parse_from_string(char *str)
{
save_scanner_state();
OpenAxiom::IOStateManager save_io_state { };
last_ch = NoChar;
last_token = 0;
input_string = str;
input_type = SourceInputKind::String;
parse_HyperDoc();
restore_scanner_state();
}

static void
Expand Down
Loading