Name

Unisyn::Parse - Parse a Unisyn expression.

Synopsis

Parse the Unisyn expression:

      𝗮𝑎𝗯

which assigns b to a. Parse this expression and print the resulting parse tree:

    my $address = Rutf8 $Lex->{sampleText}{vav};        # Source in utf8
    my $size    = StringLength V(string, $address);     # Length of source

    my $parse   = create K(address, $address),  $size;  # Create parse tree from source
       $parse->print;                                   # Print parse tree

To get:

  ok Assemble(debug=>0, eq => <<END);                   # Assemble and run
  Assign
    Term
      Variable
    Term
      Variable
  END

Description

Parse a Unisyn expression.

Version "20210818".

The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.

Create

Create a Unisyn parse of a utf8 string.

create($address, $size)

Create a new unisyn parse from a utf8 string

     Parameter  Description
  1  $address   Address of utf8 source string to parse as a variable
  2  $size      Length of string as a variable

Example:

    my $address = Rutf8 $Lex->{sampleText}{vav};                                  # Source in utf8
    my $size    = StringLength V(string, $address);                               # Length of source


    my $parse   = create K(address, $address),  $size;                            # Create parse tree from source  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲

       $parse->print;                                                             # Print parse tree

    ok Assemble(debug=>0, eq => <<END);
  Assign
    Term
      Variable
    Term
      Variable
  END

Parse

Parse Unisyn expressions

Print

Print a parse tree

print($parse)

Create a parser for an expression described by variables.

     Parameter  Description
  1  $parse     Parse tree

Example:

    my $address = Rutf8 $Lex->{sampleText}{vav};                                  # Source in utf8
    my $size    = StringLength V(string, $address);                               # Length of source

    my $parse   = create K(address, $address),  $size;                            # Create parse tree from source

       $parse->print;                                                             # Print parse tree  # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲


    ok Assemble(debug=>0, eq => <<END);
  Assign
    Term
      Variable
    Term
      Variable
  END

Hash Definitions

Unisyn::Parse Definition

Description of parse

Output fields

address

Address of source string as utf8

arena

Arena containing tree

fails

Number of failures encountered in this parse

parse

Offset to the head of the parse tree

size

Size of source string as utf8

Private Methods

getAlpha($register, $address, $index)

Load the position of a lexical item in its alphabet from the current character.

     Parameter  Description
  1  $register  Register to load
  2  $address   Address of start of string
  3  $index     Index into string

getLexicalCode($register, $address, $index)

Load the lexical code of the current character in memory into the specified register.

     Parameter  Description
  1  $register  Register to load
  2  $address   Address of start of string
  3  $index     Index into string

putLexicalCode($register, $address, $index, $code)

Put the specified lexical code into the current character in memory.

     Parameter  Description
  1  $register  Register used to load code
  2  $address   Address of string
  3  $index     Index into string
  4  $code      Code to put

loadCurrentChar()

Load the details of the character currently being processed so that we have the index of the character in the upper half of the current character and the lexical type of the character in the lowest byte.

checkStackHas($depth)

Check that we have at least the specified number of elements on the stack.

     Parameter  Description
  1  $depth     Number of elements required on the stack

pushElement()

Push the current element on to the stack.

pushEmpty()

Push the empty element on to the stack.

lexicalNameFromLetter($l)

Lexical name for a lexical item described by its letter.

     Parameter  Description
  1  $l         Letter of the lexical item

lexicalNumberFromLetter($l)

Lexical number for a lexical item described by its letter.

     Parameter  Description
  1  $l         Letter of the lexical item

new($depth, $description)

Create a new term in the parse tree rooted on the stack.

     Parameter     Description
  1  $depth        Stack depth to be converted
  2  $description  Text reason why we are creating a new term

error($message)

Die.

     Parameter  Description
  1  $message   Error message

testSet($set, $register)

Test a set of items, setting the Zero Flag is one matches else clear the Zero flag.

     Parameter  Description
  1  $set       Set of lexical letters
  2  $register  Register to test

checkSet($set)

Check that one of a set of items is on the top of the stack or complain if it is not.

     Parameter  Description
  1  $set       Set of lexical letters

reduce($priority)

Convert the longest possible expression on top of the stack into a term at the specified priority.

     Parameter  Description
  1  $priority  Priority of the operators to reduce

reduceMultiple($priority)

Reduce existing operators on the stack.

     Parameter  Description
  1  $priority  Priority of the operators to reduce

accept_a()

Assign.

accept_b()

Open.

accept_B()

Closing parenthesis.

accept_d()

Infix but not assign or semi-colon.

accept_p()

Prefix.

accept_q()

Post fix.

accept_s()

Semi colon.

accept_v()

Variable.

parseExpressionCode()

Parse the string of classified lexical items addressed by register $start of length $length. The resulting parse tree (if any) is returned in r15.

parseExpression(@parameters)

Create a parser for an expression described by variables.

     Parameter    Description
  1  @parameters  Parameters describing expression

MatchBrackets(@parameters)

Replace the low three bytes of a utf32 bracket character with 24 bits of offset to the matching opening or closing bracket. Opening brackets have even codes from 0x10 to 0x4e while the corresponding closing bracket has a code one higher.

     Parameter    Description
  1  @parameters  Parameters

ClassifyNewLines(@parameters)

Scan input string looking for opportunities to convert new lines into semi colons.

     Parameter    Description
  1  @parameters  Parameters

ClassifyWhiteSpace(@parameters)

Classify white space per: "lib/Unisyn/whiteSpace/whiteSpaceClassification.pl".

     Parameter    Description
  1  @parameters  Parameters

parseUtf8($p, @parameters)

Parse a unisyn expression encoded as utf8 and return the parse tree.

     Parameter    Description
  1  $p           Parse
  2  @parameters  Parameters

T($key, $expected, %options)

Test a parse.

     Parameter  Description
  1  $key       Key of text to be parsed
  2  $expected  Expected result
  3  %options   Options

Index

1 accept_a - Assign.

2 accept_B - Closing parenthesis.

3 accept_b - Open.

4 accept_d - Infix but not assign or semi-colon.

5 accept_p - Prefix.

6 accept_q - Post fix.

7 accept_s - Semi colon.

8 accept_v - Variable.

9 checkSet - Check that one of a set of items is on the top of the stack or complain if it is not.

10 checkStackHas - Check that we have at least the specified number of elements on the stack.

11 ClassifyNewLines - Scan input string looking for opportunities to convert new lines into semi colons.

12 ClassifyWhiteSpace - Classify white space per: "lib/Unisyn/whiteSpace/whiteSpaceClassification.

13 create - Create a new unisyn parse from a utf8 string

14 error - Die.

15 getAlpha - Load the position of a lexical item in its alphabet from the current character.

16 getLexicalCode - Load the lexical code of the current character in memory into the specified register.

17 lexicalNameFromLetter - Lexical name for a lexical item described by its letter.

18 lexicalNumberFromLetter - Lexical number for a lexical item described by its letter.

19 loadCurrentChar - Load the details of the character currently being processed so that we have the index of the character in the upper half of the current character and the lexical type of the character in the lowest byte.

20 MatchBrackets - Replace the low three bytes of a utf32 bracket character with 24 bits of offset to the matching opening or closing bracket.

21 new - Create a new term in the parse tree rooted on the stack.

22 parseExpression - Create a parser for an expression described by variables.

23 parseExpressionCode - Parse the string of classified lexical items addressed by register $start of length $length.

24 parseUtf8 - Parse a unisyn expression encoded as utf8 and return the parse tree.

25 print - Create a parser for an expression described by variables.

26 pushElement - Push the current element on to the stack.

27 pushEmpty - Push the empty element on to the stack.

28 putLexicalCode - Put the specified lexical code into the current character in memory.

29 reduce - Convert the longest possible expression on top of the stack into a term at the specified priority.

30 reduceMultiple - Reduce existing operators on the stack.

31 T - Test a parse.

32 testSet - Test a set of items, setting the Zero Flag is one matches else clear the Zero flag.

Installation

This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:

  sudo cpan install Unisyn::Parse

Author

philiprbrenan@gmail.com

http://www.appaapps.com

Copyright

Copyright (c) 2016-2021 Philip R Brenan.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.