Skip to content

Validate identifiers #4

@TechplexEngineer

Description

@TechplexEngineer

Need to validate user identifiers.
Some can be coerced, some need to be errored. Coerce whenever possible.
Need to use a combination of the Blockly naming engine and regexp.

From the ZR People:

Blockly.Blocks.requireInFunction = function() {
    if (!this.workspace) {
        // Block has been deleted.
        return;
    }
    if (this.getSurroundParent()) {
        this.setWarningText(null);
    } else {
        this.setWarningText('Place this block inside a function.');
    }
}

Blockly.Blocks.CNameValidator = function(newVar) {
    //Regex tests whether name is a valid C identifier
    if(!new RegExp('^[a-zA-Z_][a-zA-Z0-9_]*$').test(newVar)) {
        return null;
    }
  //Don't allow names that could be used as loop counters
  if(new RegExp('^index[0-9]+$').test(newVar)) {
    return null;
  }
    //Avoid clobbering global names
    if(Blockly.zr_cpp.RESERVED_WORDS_.indexOf(',' + newVar + ',') !== -1) {
        return null;
    };
    return newVar;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions