formatting, initial multiline macro support

This commit is contained in:
Joseph Poirier 2015-06-19 04:45:51 -05:00
parent 27b73e91de
commit 9316233d16
3 changed files with 152 additions and 126 deletions

View file

@ -151,7 +151,8 @@ enum LexToken {
/* 0x5b */ TokenOpenMacroBracket,
/* 0x5c */ TokenEOF,
TokenEndOfLine,
TokenEndOfFunction
TokenEndOfFunction,
TokenBackSlash
};
/* used in dynamic memory allocation */
@ -512,7 +513,7 @@ extern void LexInitParser(struct ParseState *Parser, Picoc *pc,
extern enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value,
int IncPos);
extern enum LexToken LexRawPeekToken(struct ParseState *Parser);
extern void LexToEndOfLine(struct ParseState *Parser);
extern void LexToEndOfMacro(struct ParseState *Parser);
extern void *LexCopyTokens(struct ParseState *StartParser, struct ParseState *EndParser);
extern void LexInteractiveClear(Picoc *pc, struct ParseState *Parser);
extern void LexInteractiveCompleted(Picoc *pc, struct ParseState *Parser);

41
lex.c
View file

@ -20,7 +20,8 @@
#define LEXER_INCN(l, n) ( (l)->Pos+=(n), (l)->CharacterPos+=(n) )
#define TOKEN_DATA_OFFSET (2)
#define MAX_CHAR_VALUE (255) /* maximum value which can be represented by a "char" data type */
/* maximum value which can be represented by a "char" data type */
#define MAX_CHAR_VALUE (255)
static enum LexToken LexCheckReservedWord(Picoc *pc, const char *Word);
static enum LexToken LexGetNumber(Picoc *pc, struct LexState *Lexer, struct Value *Value);
@ -32,8 +33,8 @@ static enum LexToken LexGetStringConstant(Picoc *pc, struct LexState *Lexer,
struct Value *Value, char EndChar);
static enum LexToken LexGetCharacterConstant(Picoc *pc, struct LexState *Lexer,
struct Value *Value);
static void LexSkipComment(struct LexState *Lexer, char NextChar,
enum LexToken *ReturnToken);
static void LexSkipComment(struct LexState *Lexer, char NextChar);
static void LexSkipLineCont(struct LexState *Lexer, char NextChar);
static enum LexToken LexScanGetToken(Picoc *pc, struct LexState *Lexer,
struct Value **Value);
static int LexTokenSize(enum LexToken Token);
@ -420,8 +421,7 @@ enum LexToken LexGetCharacterConstant(Picoc *pc, struct LexState *Lexer,
}
/* skip a comment - used while scanning */
void LexSkipComment(struct LexState *Lexer, char NextChar,
enum LexToken *ReturnToken)
void LexSkipComment(struct LexState *Lexer, char NextChar)
{
if (NextChar == '*') {
/* conventional C comment */
@ -429,7 +429,6 @@ void LexSkipComment(struct LexState *Lexer, char NextChar,
(*(Lexer->Pos-1) != '*' || *Lexer->Pos != '/')) {
if (*Lexer->Pos == '\n')
Lexer->EmitExtraNewlines++;
LEXER_INC(Lexer);
}
@ -444,6 +443,14 @@ void LexSkipComment(struct LexState *Lexer, char NextChar,
}
}
/* skip a line continuation - used while scanning */
void LexSkipLineCont(struct LexState *Lexer, char NextChar)
{
while (Lexer->Pos != Lexer->End && *Lexer->Pos != '\n') {
LEXER_INC(Lexer);
}
}
/* get a single token from the source - used while scanning */
enum LexToken LexScanGetToken(Picoc *pc, struct LexState *Lexer,
struct Value **Value)
@ -522,7 +529,7 @@ enum LexToken LexScanGetToken(Picoc *pc, struct LexState *Lexer,
case '/':
if (NextChar == '/' || NextChar == '*') {
LEXER_INC(Lexer);
LexSkipComment(Lexer, NextChar, &GotToken);
LexSkipComment(Lexer, NextChar);
} else
NEXTIS('=', TokenDivideAssign, TokenSlash);
break;
@ -584,6 +591,14 @@ enum LexToken LexScanGetToken(Picoc *pc, struct LexState *Lexer,
case ':':
GotToken = TokenColon;
break;
// XXX: multiline support
// case '\\':
// if (NextChar == ' ' || NextChar == '\n') {
// LEXER_INC(Lexer);
// LexSkipLineCont(Lexer, NextChar);
// } else
// LexFail(pc, Lexer, "xx illegal character '%c'", ThisChar);
// break;
default:
LexFail(pc, Lexer, "illegal character '%c'", ThisChar);
break;
@ -1087,13 +1102,23 @@ enum LexToken LexRawPeekToken(struct ParseState *Parser)
}
/* find the end of the line */
void LexToEndOfLine(struct ParseState *Parser)
void LexToEndOfMacro(struct ParseState *Parser)
{
// bool isContinued = false;
while (true) {
enum LexToken Token = (enum LexToken)*(unsigned char*)Parser->Pos;
if (Token == TokenEndOfLine || Token == TokenEOF)
return;
// XXX: multiline support
// else if (Token == TokenEndOfLine) {
// if (!isContinued)
// return;
// else
// isContinued = false;
else
// XXX: multiline support
// if (Token == TokenBackSlash)
// isContinued = true;
LexGetRawToken(Parser, NULL, true);
}
}

View file

@ -397,7 +397,7 @@ void ParseMacroDefinition(struct ParseState *Parser)
MacroNameStr = MacroName->Val->Identifier;
if (LexRawPeekToken(Parser) == TokenOpenMacroBracket) {
/* it's a parameterised macro, read the parameters */
/* it's a parameterized macro, read the parameters */
enum LexToken Token = LexGetToken(Parser, NULL, true);
struct ParseState ParamParser;
int NumParams;
@ -431,7 +431,7 @@ void ParseMacroDefinition(struct ParseState *Parser)
if (Token != TokenCloseBracket)
ProgramFail(Parser, "close bracket expected");
} else {
/* allocate a simple unparameterised macro */
/* allocate a simple unparameterized macro */
MacroValue = VariableAllocValueAndData(Parser->pc, Parser,
sizeof(struct MacroDef), false, NULL, true);
MacroValue->Val->MacroDef.NumParams = 0;
@ -440,7 +440,7 @@ void ParseMacroDefinition(struct ParseState *Parser)
/* copy the body of the macro to execute later */
ParserCopy(&MacroValue->Val->MacroDef.Body, Parser);
MacroValue->Typ = &Parser->pc->MacroType;
LexToEndOfLine(Parser);
LexToEndOfMacro(Parser);
MacroValue->Val->MacroDef.Body.Pos =
LexCopyTokens(&MacroValue->Val->MacroDef.Body, Parser);