92 lines
4.4 KiB
C
92 lines
4.4 KiB
C
// The MIT License (MIT)
|
|
//
|
|
// Copyright (C) 2026 Scott Duensing
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to
|
|
// deal in the Software without restriction, including without limitation the
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
// IN THE SOFTWARE.
|
|
|
|
// basSource.h -- BASIC source text read with the compiler's lexer
|
|
//
|
|
// What a code editor needs to know about source text without compiling
|
|
// it: its tokens, whether a position is code (not inside a string
|
|
// literal or a comment), renaming a form or control in code, the
|
|
// WithEvents variables a file declares and the Events a class module
|
|
// declares. The IDE uses these, and BASIC programs reach them through
|
|
// compiler.bas (CompTokens$, CompInCode, CompRenameInCode$,
|
|
// CompWithEvents$, CompClassEvents$). Everything works line by line on
|
|
// text that need not compile.
|
|
|
|
#ifndef BAS_SOURCE_H
|
|
#define BAS_SOURCE_H
|
|
|
|
#include "../compiler/lexer.h"
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
// What a token of BasSourceTokenFnT is
|
|
typedef enum {
|
|
BasSrcKeywordE, // a reserved word (IF, DIM, PRINT ...)
|
|
BasSrcIdentE, // a name (also the words a program may reuse: INTEGER, CLS ...)
|
|
BasSrcStringE, // a string literal, quotes included
|
|
BasSrcNumberE, // a number literal (&H and & / # suffixes included)
|
|
BasSrcDateE, // a #date# literal
|
|
BasSrcSymbolE, // an operator or punctuation (. , ; : ( ) + = ...)
|
|
BasSrcCommentE, // ' or REM up to the end of the line
|
|
BasSrcErrorE, // text the lexer cannot read (e.g. a string without its closing quote)
|
|
BasSrcKindCountE
|
|
} BasSrcKindE;
|
|
|
|
// One token: its kind, 1-based line and column, and the source text it
|
|
// covers (src + pos, len bytes).
|
|
typedef void (*BasSourceTokenFnT)(void *ctx, BasSrcKindE kind, int32_t line, int32_t col, int32_t pos, int32_t len);
|
|
|
|
// One WithEvents variable: its name and its class.
|
|
typedef void (*BasSourceWithEventsFnT)(void *ctx, const char *name, const char *className);
|
|
|
|
// One Event of a class module: its name and its parameter list text (""
|
|
// when it has none).
|
|
typedef void (*BasSourceEventFnT)(void *ctx, const char *name, const char *params);
|
|
|
|
// The Events a class module declares ([Public] Event Name[(params)]).
|
|
void basSourceClassEvents(const char *src, BasSourceEventFnT fn, void *ctx);
|
|
|
|
// True when byte offset pos of src is code: not inside a string literal
|
|
// or a comment. Only the line holding pos is read.
|
|
bool basSourceInCode(const char *src, int32_t srcLen, int32_t pos);
|
|
|
|
// The name of a BasSrcKindE as BASIC lists it ("Keyword", "Identifier" ...).
|
|
const char *basSourceKindName(BasSrcKindE kind);
|
|
|
|
// Rename references to a form or control oldName in src: oldName (any
|
|
// case) as a whole word followed by '.' (Command1.Caption,
|
|
// Form1.Command1.Text) or '_' (Command1_Click, Form1_Load). With
|
|
// codeOnly, strings and comments are left alone. Returns the new text
|
|
// (malloc'd), or NULL when nothing matched, the result would not fit an
|
|
// int32_t length or memory runs out.
|
|
char *basSourceRename(const char *src, const char *oldName, const char *newName, bool codeOnly);
|
|
|
|
// Every token of src (srcLen bytes) in order; line ends are not
|
|
// reported. Returns how many tokens were reported.
|
|
int32_t basSourceTokens(const char *src, int32_t srcLen, BasSourceTokenFnT fn, void *ctx);
|
|
|
|
// The WithEvents variables src declares (Dim / Private / Public
|
|
// WithEvents name As Class; several declarators per statement allowed).
|
|
void basSourceWithEvents(const char *src, BasSourceWithEventsFnT fn, void *ctx);
|
|
|
|
#endif // BAS_SOURCE_H
|