Recent

Author Topic: Syntax validation  (Read 695 times)

Prakash

  • Full Member
  • ***
  • Posts: 169
Syntax validation
« on: November 25, 2022, 10:13:17 pm »
I would like to build custom editor like pascal editor.

If there is any syntax error it should highlight.

How to make this type of gui

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2010
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Syntax validation
« Reply #1 on: November 25, 2022, 10:20:19 pm »
I would start here https://wiki.freepascal.org/SynEdit

Sorry, for that you do need a custom parser that only you know what is valid and what is not, highlighter just work on keywords but not like you asking to highlight error.
RTF can be used as a Memo kind of control to put your text in.
However you parsing your lines to check if they are good or not, color them red or not red.....
You can look at Lazarus Source Code to learn how the developers did their homework, it aint easy i can tell.
« Last Edit: November 25, 2022, 10:26:55 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Prakash

  • Full Member
  • ***
  • Posts: 169
Re: Syntax validation
« Reply #2 on: November 26, 2022, 09:52:41 am »
please guide us.

we want to create

if page.question.answer = "USA" {
      goto abcd
}

if (x = 1 and y= 2) or ( z=8 and p = 5 ) {
     goto xyz
}

in this case we have to validate it correct syntax customer is typing in memo or synedit


KodeZwerg

  • Hero Member
  • *****
  • Posts: 2010
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Syntax validation
« Reply #3 on: November 26, 2022, 11:16:52 am »
please guide us.
in this case we have to validate it correct syntax customer is typing in memo or synedit
You need to parse every word, order of words = syntax.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9794
  • Debugger - SynEdit - and more
    • wiki
Re: Syntax validation
« Reply #4 on: November 26, 2022, 03:06:19 pm »
Well, you can do the validation in "OkKeyUp" / "OnUtf8KeyUp" / "On[Text]Changed", when the new char has been added.
A more advisable form would be to schedule an "OnIdle" (or even IdleTimer, with 100 ms) in either of the above events.

I have the feeling though, that is not what you asked, rather you want to know how to go about parsing the text?
And (in the context of a programming language) this is an advanced topic. So you will need to do some reading up yourself.

There are several existing Parsers in Lazarus/FPC. You can look at them for learning, and some can be configured and be used as a base for your work (there is a parser in the FCL / I don't recall the name / I never used it, can't tell if it will serve your purpose).





Obviously something like an "if" is easy to parse. You then need to add some sort of context keeping (you could use a "state" / their is a design pattern / google it).
E.g., the State after "if" would be "inside bool expression".

"page" would be an unknown word, therefore treated as variable. (or you have a list keeping track of variables). .... And so forth.

You also count open/close brackets within each state block "()[]".
So when you reach "{" all "(" since the "if" must have been closed.

"{" ends the "inside bool expression". It starts a section "code block"

Sections can be nested too. You can have an "if" inside a "code block".
(Google and learn about "stacks" (FIFO lists), linked lists.  / BTW a TList works fine for FIFO)



As the text gets bigger, scanning the whole text may take more time. If it takes more than 50..100 milliseconds, the user will notice this as a lag in reaction time in your app.

So you need to find points where to save the state (context), and only scan modified parts. (You can also do work in threads, but that is on top of all else)




While the SynEdit Highlighter does not do Syntax checks, it does have the concept of saving context for each line, and therefore being able to minimize work.
It is explained in https://wiki.lazarus.freepascal.org/SynEdit_Highlighter (see "ranges" and "context" / "ranges" is just the name used in the HL to refer to context)


440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Syntax validation
« Reply #5 on: November 26, 2022, 03:30:00 pm »
we want to create

if page.question.answer = "USA" {
      goto abcd
}

if (x = 1 and y= 2) or ( z=8 and p = 5 ) {
     goto xyz
}

in this case we have to validate it correct syntax customer is typing in memo or synedit
What you're asking for is actually not trivial at all, not to mention time consuming to develop and test.

As @Martin_fr mentioned, to do what you want, you need to parse the text entered by the user (ideally, as the user enters it), once the parser determines that it has the necessary number of tokens required to make a statement then, it can validate the statement against the language's grammar.  To do that, it also has to keep track of identifiers, their scope, their type and other "details".  This step is probably best done upon the user typing a semicolon or the keyword "end" (as far as the Pascal grammar is concerned.)

If a statement is syntactically incorrect and, presuming the parser wants to continue executing, it has to follow a set of rules for syntax error recovery.  This can sometimes be very tricky. A simple, yet quite reasonable method is exposed by Per Brinch Hansen in his book "On Pascal Compilers", you can find a scanned version of that book at Hans Otten's superb web site: http://pascal.hansotten.com/per-brinch-hansen/

I'd also recommend, you have a look at SuperPascal (also available at the above link.)

That will _not_ give you a real time parser (parse as the user types) but, it most definitely points you in the right direction.  Not only you'll learn how to write a parser, you also learn about syntax error recovery, defining a language grammar and why it is crucial to have a grammar when writing a parser.

HTH.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Syntax validation
« Reply #6 on: November 26, 2022, 07:24:25 pm »
yes, it's a lot of work...

You need to build a language parser so that it can always be scanning the code as you move around inside the editor.

  Use a Popup window or some side column to show suggestions etc.
 
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018