JSPM

pascal-code-formatter

0.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q41943F
  • License ISC

A Pascal code formatter written in TypeScript

Package Exports

  • pascal-code-formatter
  • pascal-code-formatter/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pascal-code-formatter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Pascal Code Formatter

npm version License: MIT

A simple formatter for tokenized Pascal code, designed to organize tokens into structured lines with indentation information (indentation functionality still needs to be fully implemented).

Installation

You can install this package using npm:

npm install pascal-code-formatter

Usage

First, you need to get the tokens from your Pascal code. You can use a package like pascal-tokenizer for this. Then, pass the array of tokens to the formatPascalCode function.

import { formatPascalCode, FormattedPascalLine, PascalToken, TokenType } from "pascal-code-formatter";
// Or if you prefer, get the tokens from 'pascal-tokenizer'
// import { tokenize } from 'pascal-tokenizer';

// Example tokens (obtained from a tokenizer)
const pascalTokens: PascalToken[] = [
  { type: TokenType.KEYWORD, value: "program" },
  { type: TokenType.IDENTIFIER, value: "HelloWorld" },
  { type: TokenType.SYMBOL, value: ";" },
  { type: TokenType.KEYWORD, value: "begin" },
  { type: TokenType.IDENTIFIER, value: "WriteLn" },
  { type: TokenType.SYMBOL, value: "(" },
  { type: TokenType.STRING_LITERAL, value: "'Hello, World!'" },
  { type: TokenType.SYMBOL, value: ")" },
  { type: TokenType.SYMBOL, value: ";" },
  { type: TokenType.KEYWORD, value: "end" },
  { type: TokenType.SYMBOL, value: "." },
];

// Format the tokens into lines
const formattedLines: FormattedPascalLine[] = formatPascalCode(pascalTokens);

// The result will be an array of FormattedPascalLine objects
console.log(formattedLines);
/*
[
  {
    tokens: [
      { type: 'KEYWORD', value: 'program' },
      { type: 'IDENTIFIER', value: 'HelloWorld' },
      { type: 'SYMBOL', value: ';' }
    ],
    indentation: 0 // Indentation is not yet calculated
  },
  {
    tokens: [ { type: 'KEYWORD', value: 'begin' } ],
    indentation: 0 // Indentation is not yet calculated
  },
  {
    tokens: [
      { type: 'IDENTIFIER', value: 'WriteLn' },
      { type: 'SYMBOL', value: '(' },
      { type: 'STRING_LITERAL', value: "'Hello, World!'" },
      { type: 'SYMBOL', value: ')' },
      { type: 'SYMBOL', value: ';' }
    ],
    indentation: 0 // Indentation is not yet calculated
  },
  {
    tokens: [ { type: 'KEYWORD', value: 'end' }, { type: 'SYMBOL', value: '.' } ],
    indentation: 0 // Indentation is not yet calculated
  }
]
*/

// You can iterate over the lines to reconstruct the formatted code
formattedLines.forEach((line) => {
  const indent = " ".repeat(line.indentation * 2); // Example indentation (adjust as needed)
  const lineContent = line.tokens.map((token) => token.value).join(" ");
  console.log(indent + lineContent);
});

API

formatPascalCode(tokens: PascalToken[]): FormattedPascalLine[]

  • tokens: An array of PascalToken objects (like those generated by pascal-tokenizer).
  • Returns: An array of FormattedPascalLine objects. Each object contains:
    • tokens: An array of PascalToken belonging to that line.
    • indentation: A number representing the indentation level for that line (currently always 0, needs implementation).

FormattedPascalLine

Interface describing the structure of each formatted line.

interface FormattedPascalLine {
  tokens: PascalToken[];
  indentation: number;
}

PascalToken and TokenType

These types are re-exported from pascal-tokenizer for convenience. Consult the pascal-tokenizer documentation for more details on their structure.

Contributing

Contributions are welcome! If you find a bug or have a suggestion, please open an issue in the GitHub repository.

License

Distributed under the MIT License. See LICENSE for more information.