Skip to content

Create a prerequisite parser #7

@AJaccP

Description

@AJaccP

🧠 Context

Course prerequisite data in the Carleton calendar is written in natural language (e.g. "COMP 2401 (minimum grade of C-) and (COMP 2404 or SYSC 3010) and COMP 2406"). The scraper (separate ticket) will capture these strings into a prereqRaw field on each course. This ticket is the parser that converts those raw strings into the structured Prereq AST defined in src/types/course.ts.

The AST supports five node kinds — course, all (AND), any (OR), credits, and raw. The parser only needs to produce course, all, and any nodes for now. Anything it can't handle should fall back to { kind: 'raw', text: '...' } — see src/lib/validatePlan.ts and src/data/loadCourses.ts for how raw nodes are handled gracefully throughout the app (soft warning in the planner, no edges in the explorer).

COMP 3004 is a known test fixture. Its raw prereq string from the Carleton calendar and its expected AST output already exist in src/data/courses.json. Use this as your primary test case — the parser should produce an AST that matches the existing COMP 3004 prereq field exactly.

The parser is a pure function with no React dependency. It lives alongside the other pure lib utilities. It can be enhanced in the future to cover any rare edge cases, get the main functionality working first.


🛠️ Implementation Plan

  1. Create src/lib/parsePrereq.ts. The function signature should be:

    export function parsePrereq(raw: string): Prereq | null

    Returns null for an empty or missing string, a structured Prereq node otherwise.

  2. Handle the following patterns from the Carleton calendar:

    • Single course reference: COMP 1405
    • Course with minimum grade: COMP 2401 (minimum grade of C-)
    • AND of conditions: X and Y and Z
    • OR of conditions: (X or Y or Z)
    • Nested combinations, as seen in COMP 3004
    • Anything unrecognised → { kind: 'raw', text: originalString }
  3. Write tests in src/lib/parsePrereq.test.ts. At minimum:

    • Single course code parses correctly
    • Min grade is captured on a course node
    • COMP 3004's raw string produces an AST that matches the existing entry in courses.json
    • An unrecognisable string falls back to { kind: 'raw', text: '...' }
  4. Run pnpm test and pnpm typecheck to verify.


✅ Acceptance Criteria

  • parsePrereq(rawString) returns a valid Prereq or null
  • Single course references parse to { kind: 'course', code }
  • Minimum grade is captured as minGrade on a course node
  • AND conditions parse to { kind: 'all', of: [...] }
  • OR conditions parse to { kind: 'any', of: [...] }
  • COMP 3004's raw string produces an AST matching courses.json
  • Unrecognised strings fall back to { kind: 'raw', text: '...' } — no throws
  • Tests cover all of the above
  • pnpm typecheck and pnpm test pass

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

Status
In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions