Skip to content

Instantly share code, notes, and snippets.

@stefanofago73
Created November 29, 2023 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanofago73/677e257f5559ae40f9f69dd174c1ce52 to your computer and use it in GitHub Desktop.
Save stefanofago73/677e257f5559ae40f9f69dd174c1ce52 to your computer and use it in GitHub Desktop.

THE PROMPT

Act as a PHP Senior Software engineer: You master all modern PHP syntax, data structures, and constructs. temp = 0.4

You are provided with an EBFN grammar, as input, defined inside the tags """.

"""
Grammar ::= ( 
  'SINGLE-WORD' | 
  'PARAMETERISED-WORD' '('[A-Z]+')' |
  'WORD1' 'OPTIONAL-WORD'? | 
  'WORD2' ( 'WORD-CHOICE-A' | 'WORD-CHOICE-B' ) |
  'WORD3'+ )
"""

Your task is to create a DSL (Domain Specific Language) as a Fluent API using PHP code maximizing the usage of the type system and types for every element.

The PHP code to create the DSL must respect the following rules:

  • The DSL has a PHP global function as the entry point of the DSL.
  • The DSL has an "end" PHP object function of the main DSL interface as the exit point of the DSL.
  • Every DSL Keyword becomes a PHP object function but every DSL Connection, expressed by the pipe character, becomes a new PHP Interface.
  • When the DSL has a Mandatory Choice, every Keyword of that Choice is a PHP object function in the current PHP Interface. If only one Keyword is possible, then only one PHP object function exists.
  • When the DSL has an Optional Keyword, the current PHP Interface extends the next one in the grammar with all its PHP object functions.
  • When the DSL has a Repetition of Keywords, the PHP object function representing the repeatable Keyword returns the PHP interface itself, instead of the next PHP Interface in the grammar.
  • Every DSL subdefinition becomes a parameter. This will allow for recursiveness.

Verify that all paths of the grammar are covered with a code example. Create code in separate code blocks.

THE GENERATED CODE

<?php

// Entry point function for the DSL
function startDSL(): DSLInterface
{
    return new DSL();
}

// Main DSL interface
interface DSLInterface
{
    // End point function for the DSL
    public function end(): void;

    // DSL Keyword: SINGLE-WORD
    public function singleWord(): self;

    // DSL Keyword: PARAMETERISED-WORD
    public function parameterisedWord(string $parameter): self;

    // DSL Keyword: WORD1
    public function word1(): DSLWord1Interface;

    // DSL Keyword: WORD2
    public function word2(): DSLWord2Interface;

    // DSL Keyword: WORD3
    public function word3(): DSLInterface;
}

// Interface for WORD1 and its optional WORD
interface DSLWord1Interface extends DSLInterface
{
    // Optional Keyword: OPTIONAL-WORD
    public function optionalWord(): self;
}

// Interface for WORD2 and its mandatory choice between WORD-CHOICE-A and WORD-CHOICE-B
interface DSLWord2Interface extends DSLInterface
{
    // Mandatory Choice: WORD-CHOICE-A
    public function wordChoiceA(): DSLInterface;

    // Mandatory Choice: WORD-CHOICE-B
    public function wordChoiceB(): DSLInterface;
}

// DSL implementation
class DSL implements DSLInterface, DSLWord1Interface, DSLWord2Interface
{
    // Implementing DSLInterface methods
    public function end(): void
    {
        // Implementation of the end point function
        echo "DSL ended\n";
    }

    public function singleWord(): self
    {
        // Implementation for SINGLE-WORD
        echo "SINGLE-WORD\n";
        return $this;
    }

    public function parameterisedWord(string $parameter): self
    {
        // Implementation for PARAMETERISED-WORD
        echo "PARAMETERISED-WORD with parameter: $parameter\n";
        return $this;
    }

    public function word1(): DSLWord1Interface
    {
        // Implementation for WORD1
        echo "WORD1\n";
        return $this;
    }

    public function word2(): DSLWord2Interface
    {
        // Implementation for WORD2
        echo "WORD2\n";
        return $this;
    }

    public function word3(): DSLInterface
    {
        // Implementation for WORD3
        echo "WORD3\n";
        return $this;
    }

    // Implementing DSLWord1Interface methods
    public function optionalWord(): self
    {
        // Implementation for OPTIONAL-WORD
        echo "OPTIONAL-WORD\n";
        return $this;
    }

    // Implementing DSLWord2Interface methods
    public function wordChoiceA(): DSLInterface
    {
        // Implementation for WORD-CHOICE-A
        echo "WORD-CHOICE-A\n";
        return $this;
    }

    public function wordChoiceB(): DSLInterface
    {
        // Implementation for WORD-CHOICE-B
        echo "WORD-CHOICE-B\n";
        return $this;
    }
}

// Example usage covering all paths of the grammar
$start = startDSL();
$start->singleWord()
    ->parameterisedWord("Parameter")
    ->word1()
        ->optionalWord()
    ->end();

$start = startDSL();
$start->word2()
    ->wordChoiceA()
    ->end();

$start = startDSL();
$start->word2()
    ->wordChoiceB()
    ->end();

$start = startDSL();
$start->word3()
    ->word3()
    ->end();

startDSL()
    ->singleWord()
    ->parameterisedWord("Parameter")
    ->word1()
	  ->word2()
      ->wordChoiceA()
	  ->word2()
      ->wordChoiceB()
	  ->word3()
    ->word3()
  ->end();

    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment