npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

pascal-tokenizer

v0.0.3

Published

A tokenizer for Pascal programming language

Readme

Pascal Tokenizer

A simple and efficient tokenizer for the Pascal programming language, written in TypeScript.

Installation

Install the package using npm:

npm install pascal-tokenizer

Usage

Basic Usage

You can import the tokenizePascal function and optionally the PascalToken and TokenType types

Input example:

import { tokenizePascal, PascalToken, TokenType } from "pascal-tokenizer";

const pascalCode = `
program HelloWorld;
begin
  writeln('Hello, World!'); // Example comment
end.
`;

// Tokenize the code (comments skipped by default)
const tokens: PascalToken[] = tokenizePascal(pascalCode);
console.log(tokens);

// Example: Find the first keyword token
const firstKeyword = tokens.find((token) => token.type === "KEYWORD"); // You can use the imported TokenType for type checking

// Tokenize including comments
const tokensWithComments = tokenizePascal(pascalCode, false);
console.log(tokensWithComments);

Default output:

[
  { type: "KEYWORD", value: "program" },
  { type: "IDENTIFIER", value: "HelloWorld" },
  { type: "DELIMITER_SEMICOLON", value: ";" },
  { type: "KEYWORD", value: "begin" },
  { type: "IDENTIFIER", value: "writeln" },
  { type: "DELIMITER_LPAREN", value: "(" },
  { type: "STRING_LITERAL", value: "Hello, World!" },
  { type: "DELIMITER_RPAREN", value: ")" },
  { type: "DELIMITER_SEMICOLON", value: ";" },
  { type: "KEYWORD", value: "end" },
  { type: "DELIMITER_DOT", value: "." },
  { type: "EOF", value: "" },
];

Output with comments:

[
  { type: "KEYWORD", value: "program" },
  { type: "IDENTIFIER", value: "HelloWorld" },
  { type: "DELIMITER_SEMICOLON", value: ";" },
  { type: "KEYWORD", value: "begin" },
  { type: "IDENTIFIER", value: "writeln" },
  { type: "DELIMITER_LPAREN", value: "(" },
  { type: "STRING_LITERAL", value: "Hello, World!" },
  { type: "DELIMITER_RPAREN", value: ")" },
  { type: "DELIMITER_SEMICOLON", value: ";" },
  { type: "COMMENT_LINE", value: "// Example comment" },
  { type: "KEYWORD", value: "end" },
  { type: "DELIMITER_DOT", value: "." },
  { type: "EOF", value: "" },
];

Including Comments

By default, comments are skipped. If you want to include comments in the token stream:

const tokens = tokenizePascal(pascalCode, false); // false = don't skip comments

Token Structure

Each token has the following structure:

interface PascalToken {
  type: TokenType;
  value: string;
}

Token Types

The tokenizer recognizes the following token types:

| Token Type (TokenType) | Symbol / Example in Pascal | Description | | :----------------------- | :----------------------------------- | :----------------------------------------- | | Comments | | | | COMMENT_BLOCK_BRACE | { ... } | Comment delimited by braces | | COMMENT_STAR | (* ... *) | Comment delimited by parenthesis/asterisk | | COMMENT_LINE | // ... | Single-line comment (Delphi/FPC style) | | Operators | | | | OPERATOR_ASSIGN | := | Assignment | | OPERATOR_LESS_EQUAL | <= | Less than or equal to | | OPERATOR_GREATER_EQUAL | >= | Greater than or equal to | | OPERATOR_NOT_EQUAL | <> | Not equal to | | OPERATOR_RANGE | .. | Range (used in subranges, case) | | OPERATOR_PLUS | + | Addition | | OPERATOR_MINUS | - | Subtraction | | OPERATOR_MULTIPLY | * | Multiplication | | OPERATOR_DIVIDE | / | Division (real) | | OPERATOR_EQUAL | = | Equality (comparison) | | OPERATOR_LESS | < | Less than | | OPERATOR_GREATER | > | Greater than | | OPERATOR_POINTER | ^ | Pointer dereference / Pointer type | | OPERATOR_ADDRESSOF | @ | Memory address (@ operator) | | Delimiters | | | | DELIMITER_SEMICOLON | ; | Semicolon | | DELIMITER_COMMA | , | Comma | | DELIMITER_DOT | . | Dot (end of program, record access) | | DELIMITER_LPAREN | ( | Left parenthesis | | DELIMITER_RPAREN | ) | Right parenthesis | | DELIMITER_LBRACKET | [ | Left bracket (arrays) | | DELIMITER_RBRACKET | ] | Right bracket (arrays) | | DELIMITER_COLON | : | Colon (type declaration, labels) | | Literals | | | | STRING_LITERAL | 'Hello', 'A' | String literal (without quotes in value) | | BOOLEAN_LITERAL | true, false | Boolean value (case-insensitive) | | NUMBER_REAL | 1.23, -0.5, .5 | Number with decimal part | | NUMBER_INTEGER | 100, 0, -42 | Integer number | | Others | | | | KEYWORD | (See list below) | Language reserved word | | IDENTIFIER | myVariable, Counter, TMyObject | User-defined name | | EOF | | End Of File marker (internal) | | WHITESPACE | Can be used for external consumption | Unused. |

Keywords (KEYWORD) Currently Recognized (according to tokenizer.ts):

  • program
  • const
  • var
  • begin
  • end
  • if
  • then
  • else
  • while
  • do
  • for
  • to
  • downto
  • repeat
  • until
  • case
  • of
  • record
  • array
  • procedure
  • function
  • type
  • integer
  • real
  • char
  • string
  • boolean
  • uses
  • label
  • goto

(Note: true and false are specifically handled as BOOLEAN_LITERAL in your code, even though they might be in an initial keyword list).

Considerations

Please note that if you have writeln('Hello, World!'), the resulting token will be { type: "STRING_LITERAL", value: "Hello, World!" }. The single quotes (') will not be included directly in the value. This is intentional, so that you can render it on the screen as desired later on.

License

MIT