Plan 9 from Bell Labs’s /usr/web/sources/contrib/pdt/sky/cmd/skywatch/lex.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>

#include "debug.h"
#include "lex.h"
#include "sky.h"

#include "dat.h"
#include "fns.h"

State seof(Lexer *l, void *aux);
State serror(Lexer *l, void *aux);
State scmd(Lexer *l, void *aux);
State scomment(Lexer *l, void *aux);
State sswtch(Lexer *l, void *aux);
State seos(Lexer *l, void *aux);
State spopen(Lexer *l, void *aux);
State spclose(Lexer *l, void *aux);
State sidentifier(Lexer *l, void *aux);
State sdecimal(Lexer *l, void *aux);
State sstr(Lexer *l, void *aux);

State
lexstart(void)
{
	State nxt;

	nxt.fun = &sswtch;
	return nxt;
}

State
seof(Lexer *l, void *aux)
{
	State nxt;

	lemit(l, Teof);

	nxt.fun = &seof;
	return nxt;
}

State
serror(Lexer *l, void *aux)
{
	State nxt;

	if(lpeek(l) == Leof){
		nxt.fun = &seof;
		return nxt;
	}

	lemit(l, Terror);

	nxt.fun = &spopen;
	return nxt;
}

State
sswtch(Lexer *l, void *aux)
{
	State nxt;

	lacceptrun(l, L" \t\n");
	lignore(l);

	switch(lpeek(l)){
	case L'#':
		nxt.fun = &scomment;
		return nxt;
	case L'"':
		nxt.fun = &sstr;
		return nxt;
	case L'(':
		nxt.fun = &spopen;
		return nxt;
	case L')':
		nxt.fun = &spclose;
		return nxt;
	}

	if(laccept(l, L"0123456789-+.")){
		lbackup(l);
		nxt.fun = &sdecimal;
		return nxt;
	}

	if(laccept(l, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")){
		lbackup(l);
		nxt.fun = &sidentifier;
		return nxt;
	}

	nxt.fun = &serror;
	nxt.aux = "sswtch";
	return nxt;
}

State
scmd(Lexer *l, void *aux)
{
	State nxt;
	Rune *s;
	long len;

	lacceptrun(l, L" \t\n");
	lignore(l);

	lacceptuntil(l, L" \t\n)");

	if((len = llen(l)) < 0){
		nxt.fun = &serror;
		nxt.aux = "cmd";
		return nxt;
	}

	if(*l->b == L'#'){
		nxt.fun = &scomment;
		return nxt;
	}

	lemit(l, Tcmd);

	nxt.fun = &sswtch;
	return nxt;
}

State
scomment(Lexer *l, void *aux)
{
	State nxt;

	if(lacceptuntil(l, L"\n") < 0){
		nxt.fun = &serror;
		nxt.aux = "comment";
		return nxt;
	}
	laccept(l, L"\n");
	lignore(l);

	nxt.fun = &sswtch;
	return nxt;
}

State
spopen(Lexer *l, void *aux)
{
	State nxt;

	lacceptrun(l, L" \t\n");
	lignore(l);

	if(!laccept(l, L"(")){
		nxt.fun = &serror;
		nxt.aux = "popen";
		return nxt;
	}

	lemit(l, Tpopen);

	nxt.fun = &scmd;
	return nxt;
}

State
spclose(Lexer *l, void *aux)
{
	State nxt;

	lacceptrun(l, L" \t\n");
	lignore(l);

	if(!laccept(l, L")")){
		nxt.fun = &serror;
		nxt.aux = "pclose";
		return nxt;
	}

	lemit(l, Tpclose);

	nxt.fun = &sswtch;
	return nxt;
}

State
sidentifier(Lexer *l, void *aux)
{
	State nxt;

	lacceptrun(l, L" \t\n");
	lignore(l);

	if(!laccept(l, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")){
		nxt.fun = &serror;
		nxt.aux = "identifier";
		return nxt;
	}

	if(lacceptrun(l, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.0123456789") < 0){
		nxt.fun = &serror;
		nxt.aux = "identifier";
		return nxt;
	}

	lemit(l, Tidentifier);

	nxt.fun = &sswtch;
	return nxt;
}

State
sdecimal(Lexer *l, void *aux)
{
	State nxt;

	lacceptrun(l, L" \t\n");
	lignore(l);

	if(!laccept(l, L"0123456789.+-")){
		nxt.fun = &serror;
		nxt.aux = "decimal";
		return nxt;
	}
	lbackup(l);

	laccept(l, L"+-");
	lacceptrun(l, L"0123456789");
	laccept(l, L".");
	lacceptrun(l, L"0123456789");
	laccept(l, L"eE");
	laccept(l, L"+-");
	if(lacceptrun(l, L"0123456789") < 0){
		nxt.fun = &serror;
		nxt.aux = "decimal";
		return nxt;
	}

	lemit(l, Tdecimal);

	nxt.fun = &sswtch;
	return nxt;
}

State
sstr(Lexer *l, void *aux)
{
	State nxt;

	lacceptrun(l, L" \t\n");
	lignore(l);

	if(!laccept(l, L"\"")){
		nxt.fun = &serror;
		nxt.aux = "str";
		return nxt;
	}
	lignore(l);

	if(lacceptuntil(l, L"\"") < 0){
		nxt.fun = &serror;
		nxt.aux = "str";
		return nxt;
	}

	lemit(l, Tstr);
	laccept(l, L"\"");

	nxt.fun = &sswtch;
	return nxt;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].