Skip to content

mmaazahmed/MyPL

Repository files navigation

MyPL (My Playwright Language) Design Document

How To run

python interpreter.py ./path/to/file

Language Overview

Design Principles

  • English-like syntax for web automation
  • Minimalist, space-separated commands
  • Case-sensitive with specific conventions:
    • Keywords: ALL CAPS (e.g., OPEN, SET)
    • Variables: lowercase (e.g., counter, username)
    • Comments: Start with # (e.g., # This is a comment)

Supported Features

Data Types

  • Integers (e.g., 3, 42)
  • Strings (double-quoted, e.g., "hello")
  • Implicit Booleans (evaluated during runtime)

Core Functionality

  • Browser navigation
  • Element interaction (clicks, fills)
  • Text extraction
  • Automatic waiting
  • Manual waiting
  • Variables and assignments
  • Conditionals and loops
  • Error handling

Reserved Keywords

KeywordPurpose
OPENNavigate to URL
WAITPause execution
CLICKClick element
FILLFill form field
READExtract text from element
LOGPrint output
SETVariable assignment
IFConditional block
ELSEAlternative block
WHILELoop construct
TRYError handling block
CATCHError recovery block
HASString contains operator
ISEquality operator
<Less-than operator
>Greater-than operator
<=Less-than-equals operator

Language Components

ComponentExamplesPurpose
NavigationOPEN "url"Page control
Element ActionsCLICK "#btn"UI interaction
Data FlowSET var WITH valueState management
Control LogicIF (cond) { ... }Conditional execution
Error HandlingTRY { ... } CATCH { ... }Resilient execution

Syntax Specification

Command Structure

  • Space-separated tokens
  • Number of spaces between commands doesn’t matter
  • No newlines required (but supported for readability)
  • Example: OPEN "url" WAIT 3 CLICK "#btn"

Core Commands

CommandExampleDescription
OPEN "url"OPEN "https://example.com"Navigate to URL
CLICK "selector"CLICK "#submit-btn"Click element
FILL sel WITH valFILL "#email" WITH "a@b.c"Fill text field
READ "selector"SET text WITH READ ".header"Extract text from element
WAIT secondsWAIT 3Pause execution
LOG expressionLOG "Done" LOG counterPrint output

Variables

Assignment

  • SET variable WITH value
  • Examples:
    SET name WITH "Maaz"    # String assignment
    SET count WITH 5        # Integer assignment
    SET copy WITH original # Variable copying
        

Usage

  • Referenced by name (lowercase)
  • Example: LOG count FILL "#name" WITH username

Conditionals

Operators

OperatorDescriptionExample
ISEquality(var IS 5), (var IS “text”)
HASString contains(var HAS “substr”)
<Less than(var < 10)
>Greater than(var > 10)
<=Less than equals(var <= 10)

Syntax

IF (condition) {
    # commands
} ELSE {
    # commands
}

Example

SET status WITH READ "#status"
IF (status HAS "success") {
    CLICK "#next"
} ELSE {
    LOG "Operation failed"
}

Loops

Syntax

WHILE condition {
    # commands
}

Example

SET counter WITH 0
WHILE counter < 5 {
    LOG counter
    SET counter WITH counter + 1
}

Error Handling

Syntax

TRY {
    # commands
} CATCH {
    # error handling
}

Example

TRY {
    CLICK "#unstable-element"
} CATCH {
    LOG "Element not found"
    CLICK "#fallback-btn"
}

Sample Script

OPEN "https://example.com/login"
SET username WITH READ "#username-label"

IF (username HAS "Admin") {
  FILL "#user" WITH "admin@test.com"
} ELSE {
  FILL "#user" WITH "guest@test.com"
}

TRY {
  CLICK "#login-btn"
} CATCH {
  LOG "Login button missing!"
}

ON ".welcome"{

        SET welcome_text WITH READ ".welcome"

        IF (welcome_text HAS "Admin") {
        FILL "#auth" WITH "admin-pass"
        } ELSE {
        FILL "#auth" WITH "default-pass"
        }

        TRY {
        CLICK "#submit"
        WAIT 2  # Wait for navigation
        LOG "Login success!"
        } CATCH {
         LOG "Failed: element not found"
        }
}

Error Handling

Error Types

Recoverable Errors

  • Playwright errors (timeouts, missing elements)
  • MyPL runtime errors (undefined variables)

Fatal Errors

  • Syntax errors
  • Unsupported commands

Architecture

Processing Pipeline

  1. Lexer: Tokenizes input (e.g., OPENTokenType.NAVIGATE)
  2. Parser: Builds Abstract Syntax Tree (AST) from tokens
  3. Interpreter: Executes AST using Playwright and Python

Key AST Nodes

interface Program {
  type: "Program";
  body: Node[];
}

interface NavigateExpression {
  type: "NavigateExpression";
  url: Literal;
}

interface AssignmentExpression {
  type: "AssignmentExpression";
  target: Identifier;
  value: Expression;
}

interface TryCatchExpression {
  type: "TryCatchExpression";
  try: Statement[];
  catch: Statement[];
}

Interpreter Architecture

Processing Pipeline

  1. Lexical Analysis: Tokenizes source code using regex patterns
    • Example: "CLICK"(CLICK, "CLICK")
  2. Parsing: Builds AST and validates syntax
  3. Execution: Walks AST and performs actions using Playwright

Key Components

ComponentResponsibility
LexerTransforms source code into token pairs
ParserConstructs and validates the Abstract Syntax Tree (AST)
RuntimeManages state and executes the AST
Playwright AdapterHandles browser interactions using Playwright

Lexical Analysis

Token Specification

token_spec = [
    ("FILL", r'\bFILL\b'),
    ("TRY", r'\bTRY\b'),
    ("CATCH", r'\bCATCH\b'),
    ("ON", r'\bON\b'),
    ("WAIT", r'\bWAIT\b'),
    ("OPEN", r'\bOPEN\b'),
    ("READ", r'\bREAD\b'),
    ("WITH", r'\bWITH\b'),
    ("SET", r'\bSET\b'),
    ("LOG", r'\bLOG\b'),
    ("CLICK", r'\bCLICK\b'),
    ("WHILE", r'\bWHILE\b'),
    ("IF", r'IF'),
    ("ELSE", r'ELSE'),
    ("INTEGER_LITERAL", r'\b\d+\b'),
    ("STRING_LITERAL", r'"([^"]*)"'),
    ("LOGICAL_OPERATOR", r'==|\bHAS\b|\bIS\b|[<>]=?'),
    ("ARITHMETIC_OPERATOR", r'\+|\-|\*|\/'),
    ("IDENTIFIER", r'\b[a-z][a-zA-Z0-9_]*\b'),
    ("L_BRACE", r'\{'),
    ("R_BRACE", r'\}'),
    ("L_PAREN", r'\('),
    ("R_PAREN", r'\)'),
]

Lexer Workflow

  1. Input: Source code file
  2. Processing:
    • Scan stream using regex patterns
    • Filter out whitespace/comments
    • Generate token stream
  3. Output: Sequence of (token_type, value) pairs

Syntax Parsing

Parser Architecture

  • Recursive Descent: Each grammar rule has a corresponding method
  • Validation: Strict token expectations
  • AST Generation: Structured program representation in JSON

Key Parsing Methods

MethodResponsibility
parse_statement()Top-level dispatch
parse_expression()Handle values and operators
parse_block()Process { … } groups
parse_conditional()Handle IF/ELSE logic

Example AST Node

{
  "type": "ConditionalExpression",
  "conditional": {
    "type": "BinaryExpression",
    "operator": "HAS",
    "left": {"type": "Identifier", "name": "status"},
    "right": {"type": "Literal", "value": "success"}
  },
  "then": [...],
  "else": [...]
}

Interpreter Design

Execution Pipeline

  1. Initialize Playwright browser
  2. Store variables in a dictionary
  3. Traverse AST:
    • Resolve variables
    • Execute Playwright commands
    • Handle errors

Playwright Integration

  • Automatic waiting for elements
  • Direct mapping to browser actions:
    • CLICKpage.locator().click()
    • FILLpage.locator().fill()
    • READpage.locator().text_content()
    • ONpage.locator().wait_for(state ='visible')

About

MyPL is a minimalist, English-like scripting language for web automation using Playwright

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published