Purple  0.1
Standard Language Specification
Functions
scan.c File Reference

Functions for lexical scanning from input source files. More...

#include <math.h>
#include <string.h>
#include "data.h"
#include "scan.h"
#include "utils/logging.h"
Include dependency graph for scan.c:

Functions

char next (void)
 Get the next valid character from the current input file. More...
 
void put_back_into_stream (char c)
 Put a character back into the input stream. More...
 
static char skip_whitespace (void)
 Skips whitespace tokens. More...
 
static int index_of (char *s, int c)
 Return the index of char c in string s. More...
 
static int char_to_int (char c, int base)
 Convert a character into its integer form. More...
 
static Number parse_number_literal (char *literal, int length, int base)
 
static Number scan_number_literal (char c)
 Scan and return an integer literal from the input stream. More...
 
static char scan_char_literal (char c)
 Scan and return an integer literal from the input stream. More...
 
static bool is_valid_identifier_char (char c, int index)
 Determine if a character in an identifier is a permitted character. More...
 
static int scan_identifier (char c, char *buf, int max_len)
 Scan an alphanumeric identifier. More...
 
static TokenType parse_keyword (char *keyword_string)
 Retrieve the TokenType value corresponding to a keyword string. More...
 
static bool scan_check_integer_literal (char c)
 Check if the current character is the start of an integer literal. More...
 
static bool scan_check_char_literal (char c)
 Check if the current character is the start of a character literal. More...
 
static bool scan_check_keyword_identifier (char c)
 Check if the current character is the start of a keyword or identifier. More...
 
bool scan ()
 Scan tokens into the Token struct. More...
 

Detailed Description

Functions for lexical scanning from input source files.

Author
Charles Averill
Date
08-Sep-2022

Function Documentation

◆ char_to_int()

static int char_to_int ( char  c,
int  base 
)
static

Convert a character into its integer form.

Parameters
cCharacter to convert
baseBase to convert to, or -1 if no limit
Returns
int Value of character

◆ index_of()

static int index_of ( char *  s,
int  c 
)
static

Return the index of char c in string s.

Parameters
sString to search in
cCharacter to search for
Returns
int Index of c in s if s contains c, else -1

◆ is_valid_identifier_char()

static bool is_valid_identifier_char ( char  c,
int  index 
)
static

Determine if a character in an identifier is a permitted character.

Parameters
cCharacter to check
indexIndex of character in identifier string
Returns
bool True if character is permitted

◆ next()

char next ( void  )

Get the next valid character from the current input file.

Returns
char Next valid character from the current input file

◆ parse_keyword()

static TokenType parse_keyword ( char *  keyword_string)
static

Retrieve the TokenType value corresponding to a keyword string.

Parameters
keyword_stringString to convert to TokenType
Returns
TokenType TokenType of the keyword, or 0 if the keyword is not recognized

◆ parse_number_literal()

static Number parse_number_literal ( char *  literal,
int  length,
int  base 
)
static

◆ put_back_into_stream()

void put_back_into_stream ( char  c)

Put a character back into the input stream.

Parameters
cCharacter to be placed into the input stream

◆ scan()

bool scan ( )

Scan tokens into the Token struct.

Returns
bool Returns true if a Token was scanned successfully

◆ scan_char_literal()

static char scan_char_literal ( char  c)
static

Scan and return an integer literal from the input stream.

Parameters
cCurrent character
Returns
int Scanned integer literal

◆ scan_check_char_literal()

static bool scan_check_char_literal ( char  c)
static

Check if the current character is the start of a character literal.

Parameters
c
Returns
bool True if the current character is the start of a character literal

◆ scan_check_integer_literal()

static bool scan_check_integer_literal ( char  c)
static

Check if the current character is the start of an integer literal.

Parameters
cCharacter to check
Returns
bool True if the current character is the start of an integer literal

◆ scan_check_keyword_identifier()

static bool scan_check_keyword_identifier ( char  c)
static

Check if the current character is the start of a keyword or identifier.

Parameters
cCharacter to check
Returns
bool True if the current character is the start of a keyword or identifier

◆ scan_identifier()

static int scan_identifier ( char  c,
char *  buf,
int  max_len 
)
static

Scan an alphanumeric identifier.

Parameters
cFirst character in identifier
bufBuffer to read identifier into
max_lenMaximum length of the identifier
Returns
int Length of the identifier

◆ scan_number_literal()

static Number scan_number_literal ( char  c)
static

Scan and return an integer literal from the input stream.

Parameters
cCurrent character
Returns
Number Scanned number literal

◆ skip_whitespace()

static char skip_whitespace ( void  )
static

Skips whitespace tokens.

Returns
char The next non-whitespace Token